动态生成的相同name的元素验证只会取第一个.
很恼火的问题.只有将jquery.validate.js中的对相同name的元素判断注释掉.
但是不知道会不会引起其他地方的BUG
希望以后jquery.validate.js能做针对元素ID进行验证而不仅仅针对元素name验证.
方法:
将484行的代码注释掉即可
// select only the first element for each name, and only those with rules specified if ( this.name in rulesCache || !validator.objectLength($(this).rules()) )
{
return false;
}注释成
// select only the first element for each name, and only those with rules specified /*if ( this.name in rulesCache || !validator.objectLength($(this).rules()) )
{
return false;
}
*/—————————————————————————————————————————————–
<html>
<head>
<link href="style.css" rel="stylesheet">
<script src="assets/js/jquery-1.7.1.min.js"></script>
<script src="assets/js/jquery.validate.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#contact-form').validate();$(":text").each( function(){
$(this).rules( "add", {
required:true,
minlength: 2
})
});
});
</script>
</head>
<body>
<form action="" id="contact-form" class="form-horizontal">
<p>
<input type="text" name="test_a" id="a"><br>
<input type="text" name="test_a" id="b"><br>
<input type="text" name="test_a" id="c"><br>
<button type="submit" class="btn btn-primary btn-large">Submit</button>
<button type="reset" class="btn">Cancel</button>
</form>
</body>
</html>
这个表单的input 是随机生成的,所以name都是相同的,我现在要用jquery.validate.js来验证输入,现在只校验了第一id=‘a’ 的,怎么让我验证所有的?
你这么写其实是添加验证成功的了,验证会被执行,只是submit的时候不是你想要的效果。
你可以试试,输入第一个框后,在第二个框里点一下不输入再点到第三个框。
可以看到验证的逻辑被执行了。
分析一下原因:
jquery.validate 这个插件在生成rules的时候是按name来生成的,也就是说,你的表单其实只添加了一条验证rule:就是对










