如何设置无线电选项检查onload与jQuery?
需要检查是否没有设置默认值,然后设置默认值
如何设置无线电选项检查onload与jQuery?
需要检查是否没有设置默认值,然后设置默认值
当前回答
当获取无线电输入值时,请注意以下行为:
$('input[name="myRadio"]').change(function(e) { // Select the radio input group
// This returns the value of the checked radio button
// which triggered the event.
console.log( $(this).val() );
// but this will return the first radio button's value,
// regardless of checked state of the radio group.
console.log( $('input[name="myRadio"]').val() );
});
因此$('input[name="myRadio"]').val()不返回单选输入的检查值,正如您所期望的那样——它返回第一个单选按钮的值。
其他回答
我想你可以假设,这个名称是唯一的,组中的所有电台都有相同的名称。然后你可以像这样使用jQuery支持:
$("[name=gender]").val(["Male"]);
注意:传递数组很重要。
的版本:
if (!$("[name=gender]:checked").length) {
$("[name=gender]").val(["Male"]);
}
$("form input:[name=gender]").filter('[value=Male]').attr('checked', true);
不需要这些。 使用简单而古老的HTML,您可以实现您想要的。 如果你让你想检查的收音机默认是这样的: <input type='radio' name='gender' checked='true' value='Male'> 当页面加载,它会来检查。
当获取无线电输入值时,请注意以下行为:
$('input[name="myRadio"]').change(function(e) { // Select the radio input group
// This returns the value of the checked radio button
// which triggered the event.
console.log( $(this).val() );
// but this will return the first radio button's value,
// regardless of checked state of the radio group.
console.log( $('input[name="myRadio"]').val() );
});
因此$('input[name="myRadio"]').val()不返回单选输入的检查值,正如您所期望的那样——它返回第一个单选按钮的值。
单程怎么样?
$('input:radio[name="gender"]').filter('[value="Male"]').attr('checked', true);