js中获取被选中radio的值

<input type="radio" name="address" value="0" />

<input type="radio" name="address" value="1" />

<input type="radio" name="address" value="2" />

js中 : var addressID = $("input[name='address']:checked").val();

把radio的name属性值设置成一样的,就会变为单选。

1.获取选中值,三种方法都可以:

$('input:radio:checked').val();

$("input[type='radio']:checked").val();

$("input[name='id']: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为选中值:

扫描二维码关注公众号,回复: 3898615 查看本文章

$('input:radio:last').attr('checked', 'checked');

或者 $('input:radio:last').attr('checked', 'true');

4.根据索引值设置任意一个radio为选中值: $('input:radio').eq(索引值).attr('checked', 'true');索引值=0,1,2....

猜你喜欢

转载自blog.csdn.net/qq_39822451/article/details/82753282