javascript 输入元素数组的 JQuery 验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4526229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery Validation for Array of Input Elements
提问by eddy
I need to validate an array of input text elements (mileage): For example:
我需要验证一组输入文本元素(里程):例如:
<tbody>
<c:forEach items="${list}" var="item">
<tr>
<!--some other columns--->
<td align="left"><input type="text" name="mileage" value="" /></td>
</tr>
</c:forEach>
</tbody>
The script for validation is as below -
验证脚本如下 -
$(document).ready(function(){
$("#form1").validate({
rules: {
mileage: {
required: true
}
},
submitHandler: function(form) {
form.submit();
}
});
});
Now the problem is that the .validate.js only validates the first element of mileage. What can I do? How can I make the plugin validate all of the inputs text ?
现在的问题是 .validate.js 只验证里程的第一个元素。我能做什么?如何让插件验证所有输入文本?
I hope you can help me out.
我希望你能帮助我。
回答by eddy
In jquery.validate.js, we can find a function named checkForm, we have to modify it as below:
在 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();
}
回答by Luca Fagioli
Based on eddyanswer, this function takes into count also the ignoresetting.
基于涡流答案,此功能还考虑了ignore设置。
checkForm: function() {
this.prepareForm();
for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
var checkingElements = this.findByName( elements[i].name ).not(this.settings.ignore);
if (checkingElements.length !== undefined && checkingElements.length > 1) {
for (var cnt = 0; cnt < checkingElements.length; cnt++) {
this.check( checkingElements[cnt] );
}
} else {
this.check( elements[i] );
}
}
return this.valid();
},
回答by Nick Craver
You have to loop through in these cases, like this:
在这些情况下,您必须循环遍历,如下所示:
$(document).ready(function() {
$("#form1").validate({
submitHandler: function(form) {
form.submit();
}
});
$("#form1 input[name='mileage']").each(function() {
$(this).rules("add", { required: true });
});
});
.rules()only affects the first match, so you need a .each()on there to loop through and add any rules to all matches.
回答by J .
Using jquery validate 1.19.1 and modifying the jquery.validate.js file on line 465, replacing with this function should be fine.
使用jquery validate 1.19.1,修改465行的jquery.validate.js文件,用这个函数替换应该没问题。
checkForm: function() {
this.prepareForm();
for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
var checkingElements = this.findByName( elements[i].name ).not(this.settings.ignore);
if (checkingElements.length !== undefined && checkingElements.length > 1) {
for (var cnt = 0; cnt < checkingElements.length; cnt++) {
this.check( checkingElements[cnt] );
}
} else {
this.check( elements[i] );
}
}
return this.valid();
},
I hope to be of help ;)
我希望能有所帮助;)

