输入元素数组的 JQuery 验证
我需要验证输入文本元素(里程)的数组:例如:
<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>
验证脚本如下 -
$(document).ready(function(){
$("#form1").validate({
rules: {
mileage: {
required: true
}
},
submitHandler: function(form) {
form.submit();
}
});
});
现在的问题是 .validate.js 仅验证里程的第一个元素。我能做些什么?如何使插件验证所有输入文本?
我希望你能帮助我。
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 ?
I hope you can help me out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在jquery.validate.js中,我们可以找到一个名为checkForm的函数,我们需要对其进行如下修改:
In jquery.validate.js, we can find a function named checkForm, we have to modify it as below:
基于 eddy 答案,此函数还考虑了
ignore
设置。Based on eddy answer, this function takes into count also the
ignore
setting.在这些情况下,您必须循环遍历,如下所示:
.rules( )
只影响第一个匹配,所以你需要一个.each()
在那里循环遍历并向所有匹配添加任何规则。You have to loop through in these cases, like this:
.rules()
only affects the first match, so you need a.each()
on there to loop through and add any rules to all matches.使用jquery validate 1.19.1,修改第465行的jquery.validate.js文件,替换成这个函数应该就可以了。
我希望能有所帮助;)
Using jquery validate 1.19.1 and modifying the jquery.validate.js file on line 465, replacing with this function should be fine.
I hope to be of help ;)