jQuery Validate 校驗多個相同name的方法
導讀:
在表單頁中有如下代碼
<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的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
formValidator3.3的ajaxValidator一些異常分析
ajaxvalidator是大家問的最多的問題,修正一個bug(感謝網(wǎng)友“じ龍峸√”),并把大家最關心的問題,再做一次闡述。2011-07-07jQuery學習筆記之 Ajax操作篇(一) - 數(shù)據(jù)加載
Ajax 通俗來講即不需要刷新頁面即可從服務器或客戶端上加載數(shù)據(jù),當然這些數(shù)據(jù)的格式是多種多樣的。2014-06-06