如何使用jQuery将check attr添加到单选按钮nth-child?

bob*_*lon 3 jquery radio-button

我有一组单选按钮,我想将check attr添加到其中一个.这是我的代码:

<input type="radio" name="radioButton" id="rd1">
<input type="radio" name="radioButton" id="rd2">
<input type="radio" name="radioButton" id="rd3">
<input type="radio" name="radioButton" id="rd4">
<input type="radio" name="radioButton" id="rd5">

<script>
   $('input[name=slider]:nth-child(3)').prop('checked', true);
</script>
Run Code Online (Sandbox Code Playgroud)

我用.attr('checked','checked')测试了脚本.但脚本不正常.我错了什么?

pet*_*erm 7

$('input[name=radioButton]:nth-child(3)').attr('checked', true);
Run Code Online (Sandbox Code Playgroud)

或者

$('input[name=radioButton]:nth-child(3)').attr('checked', 'checked');
Run Code Online (Sandbox Code Playgroud)

这是jsFiddle


Anu*_*ith 5

尝试:

$('input[name=radioButton]:eq(2)').prop('checked', true);
Run Code Online (Sandbox Code Playgroud)