2016-11-21 159 views
0

我使用jQuery驗證的形式定義如下。從特定元素中刪除jQuery元素驗證

//#costCalculation is a form id 
$('#costCalculation').validate({ 

    onsubmit: false, //To prevent validation during form submit 
     errorClass: "my-error-class", 

    rules: { 
     adSizeFull: { 
      required: true 
     }, 
     fullAdNo: { 
      required: true 
     } 
    }, 
    messages: { 

     adSizeFull: "Please select Full adsize", 
     fullAdNo: "Please select total Ads(Full)" 
    }, 
    submitHandler: function (form) { // for demo 
     //alert('valid form'); 
     return true; 
    } 
}); 

我有這樣的

<div class="row"> 
     <div class="col-sm-6"> 
     <div class="form-group"> 
      <label class="control-label col-xs-4">Ads(Full):</label> 
      <div class="col-xs-8"> 
       <select name="adSizeFull" id="fullads-list" class="form-control" onchange="fullAdSizeChange()"> 
       <option value="">--Select--</option> 
       </select> 
      </div> 
     </div> 
     </div> 
     <div class="col-sm-6"> 
      <div class="form-group"> 
      <label class="control-label col-xs-4" for="fullAdNo">#:</label>  
      <div class="col-xs-8"> 
       <select name="fullAdNo" id="fulladnos-list" class="form-control" onchange="fullAdNoChange()"> 
       <option value="">--Select--</option> 
       </select> 
      </div> 
     </div> 
     </div> 
     </div> 

選擇框形式,我想在下面的方案中刪除驗證JS

function fullAdSizeChange() { 
    var adSizeFull = $("#fullads-list").val(); 
    var fullAdNo = $("#fulladnos-list").val(); 
    if(adSizeFull == "" && fullAdNo == "") { 
      $("#fullads-list").rules("remove", "required"); 
      $("#fulladnos-list").rules("remove", "required"); 
     } 
} 

如何從特定的元素,如果去掉驗證表單如上所述?

我還沒有複製整個代碼。語法可能有問題。我需要規範,僅實現這個

回答

0

只需添加所需的類兩個選擇框類無需添加更改事件

  <select name="adSizeFull" id="fullads-list" class="form-control required" onchange="fullAdSizeChange()"> 
      <option value="">--Select--</option> 
      </select> 
+0

這確實要刪除或驗證時改變所需的添加不鍛鍊 – Sam

+0

選擇框? –

+0

是的。我想在更改選擇框 – Sam

1

jQuery驗證居然直接支持dependency expressions

所有你需要做的是改變你這樣的驗證選項:

parent: { 
    required: function(element) { 
    return $("#age").val() < 13; 
    // Makes "parent" required only if age is below 13. 
    } 

完整的例子:

$("#myform").validate({ 
    rules: { 
    age: { 
     required: true, 
     min: 3 
    }, 
    parent: { 
     required: function(element) { 
     return $("#age").val() < 13; 
     } 
    } 
    } 
}); 
}