【发布时间】:2011-08-05 16:09:34
【问题描述】:
我尝试使用 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);
不起作用:
你还有别的想法吗?我错过了什么?
【问题讨论】:
-
感谢您的回复!我发现了问题。实际上,前两种方法是有效的。关键是我使用 jqueryUI 将一组 3 个单选按钮转换为使用以下代码设置的按钮:jQuery("#type").buttonset();但是在检查收音机之前进行此更改会破坏收音机(不知道为什么)。最后,我在检查收音机后调用了 buttonset 调用,它工作得无可挑剔。
标签: javascript jquery radio-button