jquery validate 验证插件 解决多个相同的Name 只验证第一个的方案
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了jquery validate 验证插件 解决多个相同的Name 只验证第一个的方案,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1964字,纯文字阅读大概需要3分钟。
内容图文
方案一:如果 项目里不是只是个别页面 有多个name 验证,
那么利用 prototype 来写,把这段代码加在你所要使用多个name的页面 的js初始化里 即可
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
内容总结
以上是互联网集市为您收集整理的jquery validate 验证插件 解决多个相同的Name 只验证第一个的方案全部内容,希望文章能够帮你解决jquery validate 验证插件 解决多个相同的Name 只验证第一个的方案所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。