如何使用 jQuery 检查单选按钮? [英] How to check a radio button with jQuery?
本文介绍了如何使用 jQuery 检查单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用 jQuery 检查单选按钮.这是我的代码:
</表单>
还有 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);
不起作用:
你还有别的想法吗?我错过了什么?
解决方案
对于等于或高于 (>=) 1.6 的 jQuery 版本,使用:
$("#radio_1").prop("checked", true);
对于 (<) 1.6 之前的版本,请使用:
$("#radio_1").attr('checked', 'checked');
提示:之后您可能还想在单选按钮上调用 click()
或 change()
.查看评论了解更多信息.
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?
解决方案
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.
这篇关于如何使用 jQuery 检查单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文