How can I add, remove, or swap jQuery validation rules from a page?

后端 未结 6 883
礼貌的吻别
礼貌的吻别 2020-12-04 08:42

Note:
This question refers to a very old version of jQuery.validate() (version 1.5). This plugin now provides a built-in solution for doing this: the .r

6条回答
  •  一整个雨季
    2020-12-04 09:24

    The "required" declarations inside of the validate.rules hash do not have to be statically declared as "true". You can actually supply an anonymous function instead which can check for the visibility of something and return true or false depending on the answer.

    $('form').validate({
    rules: {
      rightform_input1: { 
        required: function(){
                  return $('#leftform').css("visibility") !== "hidden"
                  } },
      rightform_input2: {
        required: function(){
                  return $('#leftform').css("visibility") !== "hidden"
                  } }
      }
    });
    

提交回复
热议问题