博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jquery validate不能验证多个相同的Name 只验证第一个的方案
阅读量:4087 次
发布时间:2019-05-25

本文共 2215 字,大约阅读时间需要 7 分钟。

方案一:如果 项目里不是只是个别页面 有多个name 验证,

那么利用 prototype 来写,把这段代码加在你所要使用多个name的页面  的js初始化里 即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
if 
($.validator) {
           
$.validator.prototype.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
;
               
});
           
}
       
}

方案二:修改源文件   这样做 就是所有的页面都可以验证多个name 

利用 ctrl+F 查找 this.name in rulesCache 找到 源码 (不是压缩过的 min.js)。把这行if判断注释即可。

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; }); },

 

如果是压缩js min.js :

查找 这段 (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
以上是转发网友的文章。试了下第一个可以用的,第二个还没试过。原文章出处:http://www.cnblogs.com/danywdd/p/5667903.html
你可能感兴趣的文章
HTML5的表单验证实例
查看>>
JavaScript入门笔记:全选功能的实现
查看>>
程序设计方法概述:从面相对象到面向功能到面向对象
查看>>
数据库事务
查看>>
JavaScript基础1:JavaScript 错误 - Throw、Try 和 Catch
查看>>
SQL基础总结——20150730
查看>>
SQL join
查看>>
JavaScript实现页面无刷新让时间走动
查看>>
CSS实例:Tab选项卡效果
查看>>
前端设计之特效表单
查看>>
前端设计之CSS布局:上中下三栏自适应高度CSS布局
查看>>
Java的时间操作玩法实例若干
查看>>
JavaScript:时间日期格式验证大全
查看>>
pinyin4j:拼音与汉字的转换实例
查看>>
XML工具代码:SAX从String字符串XML内获取指定节点或属性的值
查看>>
时间日期:获取两个日期相差几天
查看>>
责任链模式 Chain of Responsibility
查看>>
高并发与大数据解决方案概述
查看>>
解决SimpleDateFormat线程安全问题NumberFormatException: multiple points
查看>>
MySQL数据库存储引擎简介
查看>>