如何使用input
插件验证文件jquery.validate
的数组?
代码适用于单个文件,但不适用于多个上传,其中name=""
是一个数组。
JQ:
<script type="text/javascript">
$(document).ready(function(){
$("#Main").validate({
rules: {
pic: { required:true, accept: "jpg|jpeg" }
}}
);
});
</script>
单次上传:
<html>
<head>
</head>
<body>
...
<td>Fotografija 1:</td>
<td> <input type="file" class="fup" name="pic" /> </td>
...
</body>
</html>
多次上传
<html>
<head>
</head>
<body>
...
<td>Fotografija 1:</td>
<td> <input type="file" class="fup" name="pic[]" /> </td>
</tr>
<tr>
<td>Fotografija 2:</td>
<td> <input type="file" class="fup" name="pic[]" /> </td>
</tr>
<tr>
<td>Fotografija 3:</td>
<td> <input type="file" class="fup" name="pic[]" /> </td>
...
</body>
</html>
答案 0 :(得分:1)
你可以试试这个:
$("input.fup").each(function(){
$(this).rules("add", {
required:true,
accept: "jpg|jpeg"
});
});
有关详细信息,请参阅 here
Here 是另一种资源。
答案 1 :(得分:-1)
这里有一些问题
"pic[]" {your rules}
和
rules("add)
仅对数组中的第一个元素进行操作。
===
好的,我找到了解决方案:
你应该修复jquery.validate.js
查找checkForm
功能
并将其替换为:
checkForm: function() {
this.prepareForm();
for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
if (this.findByName( elements[i].name ).length != undefined && this.findByName( elements[i].name ).length > 1) {
for (var cnt = 0; cnt < this.findByName( elements[i].name ).length; cnt++) {
this.check( this.findByName( elements[i].name )[cnt] );
}
} else {
this.check( elements[i] );
}
}
return this.valid();
},