jQuery Validate は、同じ名前の複数のインスタンスの共有を検証します。

小云云
リリース: 2018-01-12 09:07:58
オリジナル
1804 人が閲覧しました

この記事では主に複数の同一名を検証するjQuery Validateの方法を紹介しますので、必要な方は参考にしていただければ幸いです。

はじめに:

フォームページには次のコードがあります

 <form>
  <input name="zhai"/><!-- 三个相同name的input -->
  <input name="zhai"/>
  <input name="zhai"/>
 </form>
ログイン後にコピー

jquery validateは、複数の同じ名前を検証するときに最初の入力ボックスのみを検証します。

解決策 1:

フォームページに対応する js に次のコードを追加します

 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;
    });
   }
  }
ログイン後にコピー

解決策 2:

ソースファイルを変更するすべてのページで複数の名前を確認できます

方法 1: jquery.validate.js ファイルを変更します

Ctrl+F を使用して rulesCache で this.name を見つけ、次のコードをコメントアウトします。

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 //添加这行
ログイン後にコピー

関連する推奨事項:

Spring hiro ブートストラップのjquery.validateログイン・登録機能の実装方法

jQuery Validateフォーム検証プラグイン詳細解説

jquery.validate.js 複数の同一名を扱う方法を詳細解説

以上がjQuery Validate は、同じ名前の複数のインスタンスの共有を検証します。の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!