首頁 > web前端 > js教程 > 主體

jQuery Validate 校驗多個name的實例詳解

Y2J
發布: 2018-05-14 16:41:01
原創
2040 人瀏覽過

這篇文章主要介紹了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 //添加这行
登入後複製

【相關推薦】

1. Javacript免費影片教學

2. #單行JS實作行動裝置金錢格式檢定

3. vue v-model表單控制項綁定的實例教學

4. Bootstrap 表單驗證formValidation 的實例詳解

5. JS中offsetWidth的bug及處理方法

以上是jQuery Validate 校驗多個name的實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!