衫裤跑路 2011-04-14 15:47 采纳率: 50%
浏览 439
已采纳

如何使用 jQuery 检查单选按钮?

I try to check a radio button with jQuery. Here's my code:

<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>

And the JavaScript:

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

Doesn't work:

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

Doesn't work:

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

Doesn't work:

Do you have another idea? What am I missing?

转载于:https://stackoverflow.com/questions/5665915/how-to-check-a-radio-button-with-jquery

  • 写回答

24条回答 默认 最新

  • 三生石@ 2011-04-14 15:49
    关注

    For versions of jQuery equal or above (>=) 1.6, use:

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

    For versions prior to (<) 1.6, use:

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

    Tip: You may also want to call click() or change() on the radio button afterwards. See comments for more info.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(23条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料