1

我正在使用 Jquery 表单验证,其基本形式如下所示

$('#shopping_form').validate({          
  submitHandler: function(){
  $.post("/url", { message: messageval}, function(data) {
  ......
  ......
  },
}
,rules: {
    message: { required:true },
    price: { number: true  }                
}
});

现在我在这个表单中添加了两个单选按钮,分别称为 radio1 和 radio2。如果选择了 radio1,则字段消息是必需的,而对于 radio2,它不是必需的字段。

我怎样才能添加这种规则。提前感谢您的帮助

4

1 回答 1

1

您可以通过编写一个自定义方法来检查单选按钮的值,并根据该值检查消息字段。这是您可以在 jQuery 中添加的代码

$.Validation.addRule("message",{
 check: function(value) {
    // Check the value of radio button here, You can do it as 
      if($("form input:radio:checked").val() == 'radio2') return true;
     // radio2 is checked then return valid, ie. this field is not required field.
    if(value.length > 0) {
        return true;
    }
    return false;
},
msg : "Field is required."
});
于 2012-09-03T18:50:59.233 回答