我正在使用Jquery表单验证,它在基本表单中如下所示
$('#shopping_form').validate({
submitHandler: function(){
$.post("/url", { message: messageval}, function(data) {
......
......
},
}
,rules: {
message: { required:true },
price: { number: true }
}
});现在,我在表单中添加了两个单选按钮,分别称为radio1和radio2。如果选择了radio1,则字段消息是必填字段,而对于radio2,它不是必填字段。
如何添加这样的规则。提前感谢您的帮助
发布于 2012-09-04 02:50:59
您可以通过编写一个自定义方法来检查单选按钮值,并根据该值检查消息字段。下面是您可以在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."
});https://stackoverflow.com/questions/3327428
复制相似问题