下面搜集了五种方法,主要还是前两个提供了解决方案,第三种需要修改jQuery源码:

修复jquery.validate插件中name属性相同(如name=’a[]‘)时验证的bug

使用jquery.validate插件http://jqueryvalidation.org/,当节点的name相同时候,脚本特意忽略剩余节点,导致所有相关节点的errMsg都显示在第一个相关节点上。这个bug在动态生成表单时候影响比较大。

通过查询资料,找到一个解决方案:

http://stackoverflow.com/questions/931687/using-jquery-validate-plugin-to-validate-multiple-form-fields-with-identical-nam

具体内容为

1 $(function ()
{
2 if ($.validator)
{
3     //fix:
when several input elements shares the same name, but has different id-ies....
4     $.validator.prototype.elements
function ()
{
5
6         var validator
this,
7             rulesCache
= {};
8
9         //
select all valid inputs inside the form (no submit or reset buttons)
10         //
workaround $Query([]).add until http://dev.jquery.com/ticket/2114 is solved
11         return $([]).add(this.currentForm.elements)
12         .filter(":input")
13         .not(":submit,
:reset, :image, [disabled]"
)
14         .not(this.settings.ignore)
15         .filter(function ()
{
16             var elementIdentification
this.id
|| 
this.name;
17
18             !elementIdentification
&& validator.settings.debug && window.console && console.error(
"%o
has no id nor name assigned"
this);
19
20             //
select only the first element for each name, and only those with rules specified
21             if (elementIdentification in rulesCache
|| !validator.objectLength($(
this).rules()))
22                 return false;
23
24             rulesCache[elementIdentification]
true;
25             return true;
26         });
27     };
28 }
29 });

在页面上引入以上代码,然后给相关节点加上id属性,当name属性相同时候会以id属性来验证

-------------------------------------------------------------------------------------------

用下面这种方式应该能解决:(http://stackoverflow.com/questions/2589670/using-jquery-validate-with-multiple-fields-of-the-same-name)
$(function(){
$("#myform").validate();
$("[name=field]").each(function(){
$(this).rules("add", {
required: true,
email: true,
messages: {
required: "Specify a valid email"
}
});
});
}); ---------------------------------------------------------------------------------- jquery.validate.js 相同name的多个元素只能验证第一个元素的解决办法
动态生成的相同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:就是对name=test_a的字段做非空和最小长度验证。 当输入框失去焦点时会触发这条规则,因为每个input的name都是test_a,可以命中rules中的规则 当submit的时候,同样会调用{'test_a': { required:true, minlength: 2}}这条规则, 只不过这条规则会被通过,因为已经有一个test_a字段达到了规则的要求。
追问
那怎么实现submit的时候全部校验呢?
回答
修改input的name, 动态生成不同的name
追问
我使用class的方式还是只检验一个啊?求解
回答
嗯,我也试了,是不行。所以建议修改name, 或者不用jq的插件
---------------------------------------------------------------------------------------------------------------------------------------------
function validate()
{
var result=true;
$("input[name='你定义的name']").each(
function(){
if($(this).val()=="")
{
alert("请输入");
$(this).focus();
result=false;
return;
}
}
);
return result;
} 参考文章:http://blog.csdn.net/zoutongyuan/article/details/28094565

最新文章

  1. Android 自动化测试 常用的命令----随时更新
  2. OA
  3. DS实验题 PlayGame Kruskal(UnionFindSet)
  4. Java之fianl修饰符
  5. AngularJS开发指南16:AngularJS构建大型Web应用详解
  6. hdu 3879 Base Station 最大权闭合图
  7. 第二篇:web之前端之css
  8. bzoj3573[Hnoi2014]米特运输
  9. jquery 学习 (二)
  10. EasyUI禁用控制方法常采用
  11. IntelliJ IDEA安装配置
  12. 《java入门第一季》之面向对象综合小案例
  13. C++STL模板库适配器之stack容器
  14. ScheduledThreadPoolExecutor线程池scheduleAtFixedRate和scheduleWithFixedDelay的区别
  15. Linux 中如何避免 rm -rf /*
  16. jQuery获得页面绝对和相对的位置
  17. centos7系统下hostname解析
  18. 第二次sprint
  19. JavaScript 基础数组循环和迭代的几种方法
  20. EditPlus5.0注册码

热门文章

  1. FivePlus——团队展示
  2. 第二次c++作业
  3. Task Class .net4.0异步编程类
  4. Web站点性能-宏观手段
  5. lilntcode-508-摆动排序
  6. lintcode-496-玩具工厂
  7. C++对象内存布局测试总结
  8. 构建一个内网的私有CA步骤
  9. 2nd 燃尽图
  10. 简易四则运算生成程序——添加GUI支持