Jquery validate验证表单时多个name相同的元素只验证第一个的问题

来源:互联网 发布:java商品信息管理系统 编辑:程序博客网 时间:2024/05/18 01:57
下面搜集了五种方法,主要还是前两个提供了解决方案,第三种需要修改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 () {
2if ($.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 
0 0
原创粉丝点击