jquery.validate多个相同name属性input只验证第一个的问题

原因就在 select only the first element for each name, and only those with rules specified,对于表单验证validate只做相同name的第一个input校验

elements: function() {
        var validator = this,
            rulesCache = {};

        // select all valid inputs inside the form (no submit or reset buttons)
        // workaround $Query([]).add until http://dev.jquery.com/ticket/2114 is solved
        return $([]).add(this.currentForm.elements)
        .filter(":input")
        .not(":submit, :reset, :image, [disabled]")
        .not( this.settings.ignore )
        .filter(function() {
            !this.name && validator.settings.debug && window.console && console.error( "%o has no name assigned", this);

            // 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;

            rulesCache[this.name] = true;
            return true;
        });
    },

解决方式一:去掉单个name的验证,这样所有的相同name都可以进行验证

if ($.validator) {
  $.validator.prototype.elements = function () {
   var validator = this,
   rulesCache = {};
   return $(this.currentForm)
   .find("input, select, textarea")
   .not(":submit, :reset, :image, [disabled]")
   .not(this.settings.ignore)
   .filter(function () {
    if (!this.name && validator.settings.debug && window.console) {
     console.error("%o has no name assigned", this);
   }
   rulesCache[this.name] = true;
   return true;
 });
 }
}

解决方式二:添加id的,只要保证id和name有一个不同就可以进行验证

if ($.validator) {
  $.validator.prototype.elements = function () {
    var validator = this,
    rulesCache = {};

        // select all valid inputs inside the form (no submit or reset buttons)
        // workaround $Query([]).add until http://dev.jquery.com/ticket/2114 is solved
        return $([]).add(this.currentForm.elements)
        .filter(":input")
        .not(":submit, :reset, :image, [disabled]")
        .not( this.settings.ignore )
        .filter(function() {
          var elementIdentification = this.id || this.name;
          !elementIdentification && validator.settings.debug && window.console && console.error( "%o has no name assigned", this);

            // select only the first element for each name, and only those with rules specified
            if ( elementIdentification in rulesCache || !validator.objectLength($(this).rules()) )
              return false;

            rulesCache[elementIdentification] = true;
            return true;
          });
      }
    }

参考:Using JQuery Validate Plugin to validate multiple form fields with identical names

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,938评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,067评论 19 139
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa阅读 8,929评论 0 6
  • 公司小白领每天从上班到下班都一直对着电脑,那么他们需要防辐射吗? 不用问我们都知道,他们要是想要多活几年就必须要防...
    雪儿love2017阅读 526评论 0 0
  • 又是一转眼只剩下40多天,一个多月没有来记录,过的很充实或者说在徘徊。 今天在知乎看到一段话,很受用,只剩这最后一...
    我航小天使阅读 244评论 1 0