jQuery验证接受方法-TypeError:无法读取未定义的属性“调用"
问题描述:
我想使用validate插件检查上传文件的类型.
I want to check the upload file type using the validate plugin.
但是我收到以下错误消息:
But I got the following error message:
Uncaught TypeError: Cannot read property 'call' of undefined.
Exception occurred when checking element file, check the 'accept' method.
这是我的表格.
<form id="upload" enctype="multipart/form-data">
<div class="control-group">
<div class="controls">
<input id="file" name="file" type="file">
</div>
<div class="form-group">
<button class="btn btn-primary" onclick="Submit()" type="button">Submit</button>
</div>
</div>
</form>
这是我的JavaScript:
And here is my JavaScript:
var Submit=function(){
var validator = $('#upload').validate({
rules:{
file: {
required: true,
accept: "audio/*, video/*"
}
},
message: {
file:"Invalid file type"
}
});
validator.form();
}
答
要使用"accept"
方法,您需要包括jQuery Validate其他方法文件.只需在验证文件之后添加它即可:
To use the "accept"
method you'll need to include the jQuery Validate additional methods file. Simply include it after your validate file:
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/additional-methods.min.js"></script>
然后尝试像这样启动验证器:
Then try initiating your validator like this instead:
$(document).ready(function(){
$("#upload").validate({
...settings...
});
});
然后从按钮中删除onClick
和type="button"
属性.
And remove the onClick
and type="button"
attribute from your button.
以下是文档: