jQuery 单选按钮检查属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9871646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
radio button checked property
提问by user1077300
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
在我的页面中,我有三个单选按钮,我想在单击<li>
. 我的点击功能正在运行,但点击时未选中收音机。我怎样才能做到这一点?我的代码在下面
$(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>
回答by Christofer Eliasson
回答by Rafay
$('#radio1').attr('checked')'
will return you the value, inorder to set the value try
将返回您的值,以便设置值尝试
$('#myContainer li').click(function() {
$('#radio1').attr('checked','checked');
});
or if you are using jquery 1.6+ use prop
或者如果您使用的是 jquery 1.6+,请使用 prop
$('#myContainer li').click(function() {
$('#radio1').prop('checked',true);
});
in your case no matter which li
you click it will check the radio with id=#radio1
在你的情况下,无论li
你点击哪个,它都会用 id= 检查收音机#radio1
in my opinion more appropriate behavior would be
在我看来,更合适的行为是
$('#myContainer li').click(function() {
$(":input",this).prop('checked',true);
});