JavaScript中如何获取被选中radio的value值?
JavaScript中 : var addressID = $("input[name='sex']:checked").val();
把radio的name属性值设置成一样的,就会变为单选。
<input type="radio" name="sex" value="0" />
<input type="radio" name="sex" value="1" />
<input type="radio" name="sex" value="2" />
1.获取选中值,三种方法都可以:
$('input:radio:checked').val();
$("input[type='radio']:checked").val();
$("input[name='sex']:checked").val();
2.设置第一个Radio为选中值:
$('input:radio:first').attr('checked', 'checked');
或者 $('input:radio:first').attr('checked', 'true');
注:attr("checked",'checked')= attr("checked", 'true')= attr("checked", true)
3.设置最后一个Radio为选中值:
$('input:radio:last').attr('checked', 'checked');
或者 $('input:radio:last').attr('checked', 'true');
4.根据索引值设置任意一个radio为选中值: $('input:radio').eq(索引值).attr('checked', 'true');索引值=0,1,2....

本文介绍在JavaScript中如何通过三种方式获取被选中的radio按钮的value值,并演示了如何使用jQuery设置单选按钮的默认选中状态。

1万+

被折叠的 条评论
为什么被折叠?



