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 29 jQuery
jquery拖动改变div大小
Jul 04 jQuery
jQuery获取复选框选中的当前行的某个字段的值
Sep 15 jQuery
jQuery代码优化方法总结
Jan 29 jQuery
jQuery实现获取选中复选框的值实例详解
Jun 28 jQuery
jquery实现搜索框功能实例详解
Jul 23 jQuery
jquery分页插件pagination使用教程
Oct 23 jQuery
jquery拖拽自动排序插件使用方法详解
Jul 20 jQuery
jQuery实现当拉动滚动条到底部加载数据的方法分析
Jan 24 jQuery
jquery实现手风琴案例
May 04 jQuery
Jquery+AJAX实现无刷新上传并重命名文件操作示例【PHP后台接收】
May 29 jQuery
jQuery+ajax实现用户登录验证
Sep 13 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
php开发文档 会员收费1期
2012/08/14 PHP
php使用filter过滤器验证邮箱 ipv6地址 url验证
2013/12/25 PHP
PHP模板引擎Smarty之配置文件在模板变量中的使用方法示例
2016/04/11 PHP
PHP实现将多个文件中的内容合并为新文件的方法示例
2017/06/10 PHP
PHP+redis实现微博的拉模型案例详解
2019/07/10 PHP
js操作时间(年-月-日 时-分-秒 星期几)
2010/06/20 Javascript
jQuery列表拖动排列具体实现
2013/11/04 Javascript
使用Chrome浏览器调试AngularJS应用的方法
2015/06/18 Javascript
JavaScript每天定时更换皮肤样式的方法
2015/07/01 Javascript
微信小程序 loading 详解及实例代码
2016/11/09 Javascript
js常用DOM方法详解
2017/02/04 Javascript
简单实现js倒计时功能
2017/02/13 Javascript
js通过keyCode值判断单击键盘上某个键,然后触发指定的事件方法
2017/02/19 Javascript
浅谈vue项目重构技术要点和总结
2018/01/23 Javascript
NodeJS安装图文教程
2018/04/19 NodeJs
javascript数组的定义及操作实例
2019/11/10 Javascript
python基础while循环及if判断的实例讲解
2017/08/25 Python
Python中sort和sorted函数代码解析
2018/01/25 Python
Python使用ConfigParser模块操作配置文件的方法
2018/06/29 Python
详解python解压压缩包的五种方法
2019/07/05 Python
Python实现朴素贝叶斯的学习与分类过程解析
2019/08/24 Python
python PIL/cv2/base64相互转换实例
2020/01/09 Python
css3 实现滚动条美化效果的实例代码
2021/01/06 HTML / CSS
阿迪达斯比利时官方商城:adidas比利时
2016/10/10 全球购物
ProBikeKit美国官网:自行车套件,跑步和铁人三项套件
2016/10/13 全球购物
Stokke美国官方网店:高级儿童家具、推车、汽车座椅和配件
2020/06/06 全球购物
.NET remoting的两种通道是什么
2016/05/31 面试题
Python中pass语句的作用是什么
2016/06/01 面试题
国际经济贸易专业自荐信
2014/06/13 职场文书
护理专科学生自荐书
2014/07/05 职场文书
纪念九一八事变演讲稿:忘记意味着背叛
2014/09/14 职场文书
优化经济发展环境工作总结
2015/08/11 职场文书
公务员岗前培训心得体会
2016/01/08 职场文书
利用Python判断整数是否是回文数的3种方法总结
2021/07/07 Python
java调用Restful接口的三种方法
2021/08/23 Java/Android