Answers:
if (!$("input[name='html_elements']:checked").val()) {
alert('Nothing is checked!');
}
else {
alert('One of the radio buttons is checked!');
}
.val() == ""并删除!,这样便可以完成所有工作。
.val()如果选中的单选按钮具有,将返回什么value=""?(尽管我想您可能会认为那是一个不太可能的情况)。
if ($("input[name='html_elements']:checked").val() == "")会提醒“什么都没有检查”
您可以执行以下操作:
var radio_buttons = $("input[name='html_elements']");
if( radio_buttons.filter(':checked').length == 0){
// None checked
} else {
// If you need to use the result you can do so without
// another (costly) jQuery selector call:
var val = radio_buttons.val();
}
if ($("input[name='html_elements']:checked").length == 0){吗?
请.length参阅http://api.jquery.com/checked-selector/
if ($('input[name="html_elements"]:checked').length === 0) alert("Not checked");
else alert("Checked");
var len = $('#your_form_id input:radio:checked').length;
if (!len) {
alert("None checked");
};
alert("checked: "+ len);
我用的很简单
的HTML
<label class="radio"><input id="job1" type="radio" name="job" value="1" checked>New Job</label>
<label class="radio"><input id="job2" type="radio" name="job" value="2">Updating Job</label>
<button type="button" class="btn btn-primary" onclick="save();">Save</button>
脚本
$('#save').on('click', function(e) {
if (job1.checked)
{
alert("New Job");
}
if (job2.checked)
{
alert("Updating Job");
}
}