在使用Jquery validate中遇到一个问题,当表单元素有name相同字段时,validate只校验表单元素name第一个值是否通过校验,比如

<input type="text" name="userName"/>
<input type="text" name="userName"/>
<input type="text" name="userName"/>
<select name="favour"></select>
<select name="favour"></select>
<select name="favour"></select>

这时候,jquery validate,只会校验第一个元素,看了一下源码(https://cdn.bootcss.com/jquery-validate/1.17.0/jquery.validate.js),是因为它在获取form元素时,做了一层cache处理。

elements: function() {
var validator = this,
rulesCache = {}; // Select all valid inputs inside the form (no submit or reset buttons)
return $( this.currentForm )
.find( "input, select, textarea, [contenteditable]" )
.not( ":submit, :reset, :image, :disabled" )
.not( this.settings.ignore )
.filter( function() {
var name = this.name || $( this ).attr( "name" ); // For contenteditable
if ( !name && validator.settings.debug && window.console ) {
console.error( "%o has no name assigned", this );
} // Set form expando on contenteditable
if ( this.hasAttribute( "contenteditable" ) ) {
this.form = $( this ).closest( "form" )[ 0 ];
this.name = name;
} // Select only the first element for each name, and only those with rules specified,缓存判断
if ( name in rulesCache || !validator.objectLength( $( this ).rules() ) ) {
return false;
}
// 在这里做了一层Cache,如果下次再来相同的Name,则不会再进入validate的校验List
rulesCache[ name ] = true;
return true;
} );
},

解决办法

1. 我们可以更改源码cache的key的名字或者在Jquery页面load完毕后,修改property上的validate原型方法,它使用name可能会造成这种bug,那我们使用Id,一般Id是不会重复的,如果Id不存在,我们再使用name来做为cache的key,简单来说,就是一行代码

var name = this.id || this.name || $( this ).attr( "name" ); // For contenteditable

比原有的

var name = this.name || $( this ).attr( "name" ); // For contenteditable

多了一个 this.id 判断。

2. 页面加载完成后修改的方式:

$(function() {
if ($.validator) {
//fix: when several input elements shares the same name, but has different id-ies....
$.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() {
// 这里加入ID判断
var elementIdentification = this.id || this.name; ! elementIdentification && validator.settings.debug && window.console && console.error("%o has no id nor 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;
});
};
}
});

记得给你name相同的表单元素添加id哦~

参考:http://www.jb51.net/article/101085.htm

JqueryValidate表单相同Name不校验问题解决的更多相关文章

  1. JqueryValidate 表单验证插件

    1.适用场景 表单 ( 支持自定义规则 ) 2.相关文章 jQuery Validate 3.实际问题 JqueryValidate表单相同Name不校验问题解决

  2. jQuery Validate 表单验证插件----自定义校验结果样式

    一.下载依赖包 网盘下载:https://yunpan.cn/cryvgGGAQ3DSW  访问密码 f224 二.引入依赖包 <script src="../../scripts/j ...

  3. 微信小程序——仿jqueryValidate表单验证插件WxValidate的二次封装(一)

    在做web开发时,表单验证插件我们前端用的是jqueryValidate,由于个人主要精力是在后台JAVA开发上,为了让插件与后台更好的结合和使用,通过JAVA的自定义组件将表单全部重新写了一边,同时 ...

  4. 在表单提交之前做校验-利用jQuery的submit方法

    点击表单中的提交按钮,默认就会提交表单,如果要在表单提交之前做一些校验,那么就可以用jQuery的submit方法. 也就是jQuery的submit的方法执行顺序,在表单的提交之前.用法如下: $( ...

  5. 【PHP】(原创)之表单FORM的formhash校验,以TP3.2示例

    1.目的:每次表单POST提交(ajax的POST也适用)过来数据,都必须校验formhash参数是否和服务器端的一致,不一致说明重复提交或者 跨站攻击提交csrf 2.原理:参照了 KPPW 的fo ...

  6. Jquery Validate表单验证,自定义校验正整数

    // 添加自定义校验规则,校验正整数 jQuery.validator.addMethod("positiveinteger", function(value, element) ...

  7. 优化表单数据的JS校验

    在平常的web开发中,我经常需要在客户端对表单的数据进行验证.比如,我们验证表单输入的内容不为空: ? <form action="" method="post&q ...

  8. html 表单input录入内容校验

    <p>文本框只能输入数字代码(小数点也不能输入)</p><input onkeyup="this.value=this.value.replace(/\D/g, ...

  9. 一个简单的jquery ajax表单提交 带数据校验 layer弹框提示

    <input type="hidden" id="url" value="index.php"/> <form id=&q ...

随机推荐

  1. linux socket TCP UDP bind 同义IP和port

    //TCP and UDP can bind to the same IP & port. #include <sys/types.h> #include <sys/sock ...

  2. 浅析CentOS和RedHat Linux的区别

    CentOS的简介 CentOS是Community ENTerprise Operating System的简称,我们有很多人叫它社区企业操作系统,不管你怎么叫它,它都是Linux操作系统的一个发行 ...

  3. 选股:“均线是水,K线是舟,量是马达!”的选美理念!

    选股:“均线是水,K线是舟,量是马达!”的选美理念! 很多庄家就是故意做数据,让某只股票的数据非常符合“理论”,引诱“技术派”股民

  4. Java Calendar,Date,DateFormat,TimeZone,Locale等时间相关内容的认知和使用(7) TimeZone

    本章介绍TimeZone. TimeZone 简介 TimeZone 表示时区偏移量,也可以计算夏令时.在操作 Date, Calendar等表示日期/时间的对象时,经常会用到TimeZone:因为不 ...

  5. C#编程(三十八)----------运算符

    原文链接: http://blog.csdn.net/shanyongxu/article/details/46877353 运算符 类别 运算符 算术运算符 + - * / 逻辑运算符 &  ...

  6. 【CentOS】centos如何修改你的主机名

    转载地址:https://www.linuxidc.com/Linux/2014-11/109238.htm ============================================= ...

  7. cloudera项目源代码

    以下项目都需要安装git,Linux的git还是比较容易安装的,windows的git安装参考项目区域:软件版本控制-在Windows中使用Git视频介绍 git相关软件安装参考win7安装 git软 ...

  8. actuator监控

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...

  9. 用开源项目CropImage实现图片的裁剪(不推荐)

       之前介绍过一个截图的办法(http://www.cnblogs.com/tianzhijiexian/p/3900241.html),这里再分享个开源项目.它也是截图,但是效果不是很好,首先还是 ...

  10. 深入分析ReentrantLock公平锁和非公平锁的区别

    在ReentrantLock中包含了公平锁和非公平锁两种锁,通过查看源码可以看到这两种锁都是继承自Sync,而Sync又继承自AbstractQueuedSynchronizer,而AbstractQ ...