简体   繁体   English

从特定元素中删除jQuery元素验证

[英]Remove jQuery element validation from a specific element

I have form with jQuery validation defined as follows. 我有定义如下的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;
    }
});

I have a form with select box like this 我有一个带有选择框的表格

<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>

I would like to remove validation in the following scenario JS 我想在以下场景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");  
        }
}

How to remove the validation from specific elements if the form is specified as above?? 如果表单如上所述指定,如何从特定元素中删除验证?

I haven't copied the entire code. 我没有复制整个代码。 There may problems in syntaxes. 语法可能存在问题。 I need guidelines only to implement this 我只需要准则即可实施

jQuery Validate actually directly supports dependency expressions . jQuery Validate实际上直接支持依赖项表达式

All you need to do is change your validate options like this: 您需要做的就是更改您的验证选项,如下所示:

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

Full Example: 完整示例:

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

just add the required class in both select box class no need to add change event 只需在两个选择框类中添加所需的类,无需添加更改事件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM