jQuery Validate 校验多个相同name的方法
导读:
在表单页中有如下代码
jqueryvalidate在对多个相同name校验时,只校验第一个input框。
解决方案一:
在表单页对应的js中加入如下代码只有当前页可以解决对多个name校验
if($.validator){
$.validator.prototype.elements=function(){
varvalidator=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("%ohasnonameassigned",this);
}
rulesCache[this.name]=true;
returntrue;
});
}
}
解决方案二:
修改源文件所有的页面都可以验证多个name
方式1:修改jquery.validate.js文件
用ctrl+F查找this.nameinrulesCache注释掉如下代码。
elements:function(){
varvalidator=this,
rulesCache={};
//selectallvalidinputsinsidetheform(nosubmitorresetbuttons)
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("%ohasnonameassigned",this);
}
//注释掉这里
//selectonlythefirstelementforeachname,andonlythosewithrulesspecified
//if(this.nameinrulesCache||!validator.objectLength($(this).rules())){
//returnfalse;
//}
rulesCache[this.name]=true;
returntrue;
});
},
方式2:修改jquery.validate.min.js文件
用ctrl+F查找(c[this.name]=!0,!0)})
return!this.name&&b.settings.debug&&window.console&&console.error("%ohasnonameassigned",this),
//this.nameinc||!b.objectLength(a(this).rules())?!1:(c[this.name]=!0,!0)//注释这行
c[this.name]=!0,!0//添加这行
以上所述是小编给大家介绍的jQueryValidate校验多个相同name的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!