jQuery Validate 校验多个相同name的方法


Posted in jQuery onMay 18, 2017

导读:

在表单页中有如下代码

<form>
  <input name="zhai"/><!-- 三个相同name的input -->
  <input name="zhai"/>
  <input name="zhai"/>
 </form>

jquery validate在对多个相同name校验时,只校验第一个input框。

解决方案一:

在表单页对应的js中加入如下代码 只有当前页可以解决对多个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;
    });
   }
  }

解决方案二:

修改源文件 所有的页面都可以验证多个name

方式1:修改jquery.validate.js文件

用 ctrl+F 查找 this.name in rulesCache 注释掉如下代码。

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")
   .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);
    }
    // 注释掉这里
    // 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;
   });
  },

方式2:修改jquery.validate.min.js文件

用 ctrl+F 查找(c[this.name]=!0,!0)})

return !this.name && b.settings.debug && window.console && console.error("%o has no name assigned", this),
//this.name in c || !b.objectLength(a(this).rules()) ? !1 : (c[this.name] = !0, !0)//注释这行
c[this.name] = !0, !0 //添加这行

以上所述是小编给大家介绍的jQuery Validate 校验多个相同name的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

jQuery 相关文章推荐
jquery 校验中国身份证号码实例详解
Apr 11 jQuery
jQuery 实现图片的依次加载图片功能
Jul 06 jQuery
JQuery 获取Dom元素的实例讲解
Jul 08 jQuery
jQuery实现对网页节点的增删改查功能示例
Sep 18 jQuery
jquery在启动页面时,自动加载数据的实例
Jan 22 jQuery
20个最常见的jQuery面试问题及答案
May 23 jQuery
jQuery实现常见的隐藏与展示列表效果示例
Jun 04 jQuery
jQuery实现监听下拉框选中内容发生改变操作示例
Jul 13 jQuery
jQuery扩展方法实现Form表单与Json互相转换的实例代码
Sep 05 jQuery
jQuery实现的3D版图片轮播示例【滑动轮播】
Jan 18 jQuery
JQuery中DOM节点的操作与访问方法实例分析
Dec 23 jQuery
jQuery加PHP实现图片上传并提交的示例代码
Jul 16 jQuery
jQuery Validate 无法验证 chosen-select元素的解决方法
May 17 #jQuery
jQuery查找dom的几种方法效率详解
May 17 #jQuery
jQuery实现div跟随鼠标移动
Aug 20 #jQuery
jquery+ajax实现省市区三级联动 (封装和不封装两种方式)
May 15 #jQuery
Jquery把获取到的input值转换成json
May 15 #jQuery
jQuery实现radio第一次点击选中第二次点击取消功能
May 15 #jQuery
jQuery返回定位插件详解
May 15 #jQuery
You might like
PHP面向对象编程快速入门
2006/10/09 PHP
使用TinyButStrong模板引擎来做WEB开发
2007/03/16 PHP
PHP中MVC模式的模板引擎开发经验分享
2011/03/23 PHP
PHP has encountered a Stack overflow问题解决方法
2014/11/03 PHP
php中使用base HTTP验证的方法
2015/04/20 PHP
PHP获取对象属性的三种方法实例分析
2019/01/03 PHP
不用ajax实现点击文字即可编辑的方法
2007/12/16 Javascript
javascript内置对象arguments详解
2014/03/16 Javascript
jQuery判断div随滚动条滚动到一定位置后停止
2014/04/02 Javascript
js取得html iframe中的元素和变量值
2014/06/30 Javascript
js实现文本框选中的方法
2015/05/26 Javascript
Javascript中String的常用方法实例分析
2015/06/13 Javascript
什么是JavaScript注入攻击?
2016/09/14 Javascript
bootstrap中使用google prettify让代码高亮的方法
2016/10/21 Javascript
jquery Ajax 全局调用封装实例详解
2017/01/16 Javascript
使用vue-resource进行数据交互的实例
2017/09/02 Javascript
Three.js如何实现雾化效果示例代码
2017/09/27 Javascript
在Vue mounted方法中使用data变量详解
2019/11/05 Javascript
JavaScript实现简单动态表格
2020/12/02 Javascript
python连接MySQL数据库实例分析
2015/05/12 Python
简单实现python画圆功能
2018/01/25 Python
python: 判断tuple、list、dict是否为空的方法
2018/10/22 Python
Django unittest 设置跳过某些case的方法
2018/12/26 Python
python对象与json相互转换的方法
2019/05/07 Python
Python实现报警信息实时发送至邮箱功能(实例代码)
2019/11/11 Python
浅谈matplotlib.pyplot与axes的关系
2020/03/06 Python
构建高效课堂实施方案
2014/03/13 职场文书
十周年庆典策划方案
2014/06/03 职场文书
幼儿生日活动方案
2014/08/27 职场文书
试用期转正后的自我评价
2014/09/21 职场文书
祖国在我心中演讲稿600字
2014/09/23 职场文书
村党支部书记四风问题个人对照检查材料思想汇报
2014/10/06 职场文书
行政文员岗位职责
2015/02/04 职场文书
工地食品安全责任书
2015/05/09 职场文书
初中语文教师研修日志
2015/11/13 职场文书
喜迎建国70周年:有关爱国的名言名句
2019/09/24 职场文书