2010-07-25 132 views
1

我使用jQuery表單驗證它的基本形式是這樣的JQuery的表單驗證 - 基於其他表單字段驗證表單

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

現在我加入了兩個單選按鈕,以這種形式叫他們「米收音機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." 
});