2017-02-02 5 views
0

私はフォームが4つのボタンを持っていますが、それぞれのサブミットのヒットはのControllerにあります。私が達成しようとしている何異なるボタンの異なるタイプの検証

$('.store-action').click(function (e) { 
    var ignoreValidationElements = [], 
     id = $(this).attr("id"), 
     name = $(this).attr("name"), 
     action, 
     serialNumber = $('#serial').val(), 
     param; 

    switch (id) { 
     case "submit-all": 
      action = submitUrl; 
      ignoreValidationElements.length = 0; 
      if ($('#collapsTwo').is(':visible')) { 
       $('#collapsTwo input').each(function (index, element) { 
        var innerId = $(element).attr("id"); 
        ignoreValidationElements.push(innerId); 
       }); 
      } else { 
       ignoreValidationElements.push("serial"); 
      } 
      break; 
     case "add-item": 
      action = addItemUrl; 
      ignoreValidationElements.length = 0; 
      ignoreValidationElements.push("cID"); 
      ignoreValidationElements.push("cAdd"); 
      ignoreValidationElements.push("cNum"); 
      ignoreValidationElements.push("cEmail"); 
      break; 
     case "save-all": 
      action = saveUrl; 
      ignoreValidationElements.length = 0; 
      ignoreValidationElements.push("cID"); 
      ignoreValidationElements.push("cAdd"); 
      ignoreValidationElements.push("cNumer"); 
      ignoreValidationElements.push("cEmail"); 
      if ($('#collapsTwo').is(':visible')) { 
       $('#collapsTwo input').each(function (index, element) { 
        var innerId = $(element).attr("id"); 
        ignoreValidationElements.push(innerId); 
       }); 
      } else { 
       ignoreValidationElements.push("serial"); 
      } 
      break; 
     case "cancel": 
      action = cancelUrl; 
      ignoreValidationElements.length = 0; 
      ignoreValidationElements.push("cID"); 
      ignoreValidationElements.push("cAdd"); 
      ignoreValidationElements.push("cNumb"); 
      ignoreValidationElements.push("cEmail"); 
      if ($('#collapsTwo').is(':visible')) { 
       $('#collapsTwo input').each(function (index, element) { 
        var innerId = $(element).attr("id"); 
        ignoreValidationElements.push(innerId); 
       }); 
      } else { 
       ignoreValidationElements.push("serial"); 
      } 
      break; 
    } 

がどのI arrayignoreValidationElementsに項目を追加することです:各ボタンについて、私は次のように私は同じclassとボタンのclickイベントを持っているように、異なる入力を検証する必要がクリックフォーム送信時の検証を無視できます。

#submit-allがクリックされた場合は、フォームの下半分を無視します。 save-allまたはcancelをクリックした場合は、フォームの検証全体を無視します。 add-itemをクリックした場合は、フォーム検証の上半分を無視します。ご覧のとおり、私は同じコードを何度も繰り返し書いています。私の質問は、このシナリオのために最小限のコードを書くにはどうすればいいのですか?

+0

をあなたのアプローチと間違って何もありませんが、私は、各機能が実行するアプローチを見つけると言っている...よく...機能:)よりエレガントで読みやすいです。あなたの店舗の様子はよく分かりませんが、異なるアクション(とボタン)のためのハンドラーが全く違っていて、普通のコード(もしあれば)をこれらのハンドラーから呼び出された適切な名前の関数に入れています。もちろん、スタイルと味は異なります、それはちょうど意見です。 –

答えて

1

これを試してみてください:

function ignoreValidation(ignoreValidationElements) { 
    ignoreValidationElements.length = 0; 
    ignoreValidationElements.push("cID"); 
    ignoreValidationElements.push("cAdd"); 
    ignoreValidationElements.push("cNum"); 
    ignoreValidationElements.push("cEmail"); 
} 

$('.store-action').click(function (e) { 
    var ignoreValidationElements = [], 
     id = $(this).attr("id"), 
     name = $(this).attr("name"), 
     action, 
     serialNumber = $('#serial').val(), 
     param; 

    switch (id) { 
     case "submit-all": 
      action = submitUrl; 
      ignoreValidationElements.length = 0; 
      if ($('#collapsTwo').is(':visible')) { 
       $('#collapsTwo input').each(function (index, element) { 
        var innerId = $(element).attr("id"); 
        ignoreValidationElements.push(innerId); 
       }); 
      } else { 
       ignoreValidationElements.push("serial"); 
      } 
      break; 
     case "add-item": 
      action = addItemUrl; 
      ignoreValidation(ignoreValidationElements); 
      break; 
     case "save-all": 
     case "cancel": 
      action = (id == "save-all") ? saveUrl : cancelUrl; 
      ignoreValidation(ignoreValidationElements); 
      if ($('#collapsTwo').is(':visible')) { 
       $('#collapsTwo input').each(function (index, element) { 
        var innerId = $(element).attr("id"); 
        ignoreValidationElements.push(innerId); 
       }); 
      } else { 
       ignoreValidationElements.push("serial"); 
      } 
      break; 
     } 
+0

ありがとう、私はこれを試し、私がどのように乗っているかを知らせます – Code

関連する問題