jquery validate 驗證插件 解決多個相同的Name 只驗證第一個的方案


方案一:如果 項目里不是只是個別頁面 有多個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

 

 

以上是網上找的解決方案,在這里備着,以便日后。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM