开发者

Jquery validate rule - how to require an option to be selected before validation

开发者 https://www.devze.com 2023-02-25 04:36 出处:网络
I want some input data to be validated only if specific option in a dropdown 开发者_JS百科is selected. For radiobutton this is done with such rule:

I want some input data to be validated only if specific option in a dropdown 开发者_JS百科is selected. For radiobutton this is done with such rule:

required : "#radioId:checked"

How can this be done for an option in dropdown?


If memoery serves, you can have a callback that returns whether the option you want to see selected is selected:

required: function(element) {
    return $("#selectID>option:selected").val() == 18;//or whatever value you require
  }


Another option could be to write your own selector:

    $.extend($.expr[":"], {
    myselector: function (e)
    {
        return $(e).val() == 'mine';
    }
});

Selector:

required : '#test:myselector'


You can use the depends checker with something like:

<select name="selectBox" id="selectBox">
 <option value="option1">Option 1</option>
 <option value="option2">Option 2</option>
</select>

<input type="text" name="inputBox" />

----------------------in your rules do---------------------

 inputBox: {
   depends: function (elt) {
     return $('#selectBox').val() === "option1"; 
     // option1 being the targeted option here
   }
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号