我尝试用jQuery检查单选按钮。这是我的代码:

<form>
    <div id='type'>
        <input type='radio' id='radio_1' name='type' value='1' />
        <input type='radio' id='radio_2' name='type' value='2' />
        <input type='radio' id='radio_3' name='type' value='3' /> 
    </div>
</form>

JavaScript:

jQuery("#radio_1").attr('checked', true);

不工作:

jQuery("input[value='1']").attr('checked', true);

不工作:

jQuery('input:radio[name="type"]').filter('[value="1"]').attr('checked', true);

不工作:

你还有别的主意吗?我错过了什么?


当前回答

试试这个

 $("input:checked", "#radioButton").val()

if选中返回True 如果未选中则返回False

jQuery v1.10.1

其他回答

Attr接受两个字符串。

正确的做法是:

jQuery("#radio_1").attr('checked', 'true');

对于jQuery等于或高于(>=)1.6的版本,请使用:

$("#radio_1").prop("checked", true);

对于(<)1.6之前的版本,使用:

$("#radio_1").attr('checked', 'checked');

提示:您可能还想随后在单选按钮上调用click()或change()。查看评论了解更多信息。

$("input[name=inputname]:radio").click(function() {
    if($(this).attr("value")=="yes") {
        $(".inputclassname").show();
    }
    if($(this).attr("value")=="no") {
        $(".inputclassname").hide();
    }
});

试试这个

 $("input:checked", "#radioButton").val()

if选中返回True 如果未选中则返回False

jQuery v1.10.1

我也遇到过类似的问题,一个简单的解决方法就是:

.click()

如果在调用函数后刷新radio,则任何其他解决方案都将工作。