JQ 获取input type=radio属性的value值

Demo代码:<div class="form-group"&n...

Demo代码:


<div class="form-group" style="width: 250px;margin:0 auto;">
<label for="">性别</label>
<input type="radio" class="sex" id="sex-man" name="man" value="男"/>男
<input type="radio" class="sex" id="sex-woman" name="woman" value="女"/>女
</div>


JQ:

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

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

//设置第一个Radio为选中值:

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

//或者

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

//注:attr("checked",'checked')= attr("checked", 'true')= attr("checked", true)

//设置最后一个Radio为选中值:

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

//或者

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

//根据索引值设置任意一个radio为选中值:

$('input:radio').eq('索引值').attr('checked', 'true');

//索引值=0,1,2....


评论