单选按钮选中的属性 [英] radio button checked property
本文介绍了单选按钮选中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的页面中,我有三个单选按钮,单击<li>
时要检查单选按钮.我的点击功能正在运行,但单击时未选中该收音机.我怎样才能做到这一点?我的代码在下面
In my page I have three radio buttons and I want to checked the radio buttons when clicked to the <li>
. My click function is working but when clicked the radio is not checked. How can I do that? My code is below
$(document).ready(function() {
$('#myContainer li').click(function() {
$('#radio1').attr('checked');
});
});
<div id="myContainer">
<li id="li1">
<input id="radio1" name="RadioGroup" value="val1" type="radio">
val1
</li>
<li id="li2">
<input id="radio2" name="RadioGroup" value="val2" type="radio">
val2
</li>
<li id="li3">
<input id="radio3" name="RadioGroup" value="val3" type="radio">
val3
</li>
</div>
推荐答案
您正在获取选中的属性,而不是对其进行设置.尝试以下方法:
You are getting the checked attribute instead of setting it. Try this instead:
$('#radio1').attr('checked', 'checked');
注意
从jQuery 1.6开始,建议使用 .prop()
而不是
Since jQuery 1.6 it is recommended to use .prop()
instead of .attr()
.
$("#radio1").prop("checked", true);
这篇关于单选按钮选中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文