使用jQuery多次更改单选按钮

时间:2014-04-14 12:57:30

标签: jquery radio-button

我似乎无法正确使用jQuery在单选按钮之间进行更改。这是jQuery中的错误吗?示例:http://jsbin.com/yafik/1/edit

来自jsbin的代码:

<input type="radio" name="test">
<input type="radio" name="test">
<input type="radio" name="test">
<input type="radio" name="test">

<script>
    $("input").eq(1).attr("checked", true);
    $("input").eq(2).attr("checked", true);
    //$("input").attr("checked", false);
    $("input").eq(1).attr("checked", true);
</script>

您还可以尝试取消注释第三行:$("input").attr("checked", false);,我希望重置所有已检查的属性。

我希望它能够设置第二个radiobutton进行检查。但是如果你看一下源代码,实际上都会检查第二和第三个radiobuttons。

1 个答案:

答案 0 :(得分:1)

使用.prop()作为布尔值

$("input").eq(1).prop("checked", true);
$("input").eq(2).prop("checked", true);
$("input").eq(1).prop("checked", true);
相关问题