我想验证一个表单,其控件组的输入为type="radio"
,如下所示:
<form id="sendMessageFormContainer" action="#">
@Html.ValidationSummary()
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>To:</legend>
<div id="messageToContainer">
<input type="radio" name="radio-choice-1" id="radio-choice-1" value="1" />
<label for="radio-choice-1">foo</label>
<input type="radio" name="radio-choice-1" id="radio-choice-2" value="2" />
<label for="radio-choice-2">bar</label>
</div>
</fieldset>
</div>
//...
<input id="sendMsgBtn" name="sendMsgBtnName" type="submit" value="Send" />
</form>
我正在使用validate()
方法来验证其余元素,但我不知道应该使用哪条规则来检查是否应该选择至少一个单选按钮:
$(document).on("pageshow", function () {
$("#sendMessageFormContainer").validate({
rules: {
//???
},
messages: {
//...
},
submitHandler: function (form) {
alert("Call Send Action");
}
});
});
答案 0 :(得分:2)
您需要向任何无线电输入添加至少一个class =“required”,如下所示:
<input type="radio" name="radio-choice-1" id="radio-choice-1" value="1" class="required" />
<label for="radio-choice-1">foo</label>
<input type="radio" name="radio-choice-1" id="radio-choice-2" value="2" />
<label for="radio-choice-2">bar</label>
工作示例:http://jsfiddle.net/Gajotres/a8cJA/
或者你可以这样做:
$(document).on('pagebeforeshow', '#index', function(){
$("#sendMessageFormContainer").validate({
rules : {
"radio-choice-1" : { required : true }
},
messages : {
"radio-choice-1" : "Please select radio button"
},
submitHandler: function (form) {
alert("Call Send Action");
}
});
$(document).on('click', '#sendMsgBtn', function(){
$("#sendMessageFormContainer").validate().form();
});
});