Javascript 如何使用 jQuery 检查单选按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5665915/
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
How to check a radio button with jQuery?
提问by Alexis
I try to check a radio button with jQuery. Here's my code:
我尝试使用 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>
And the JavaScript:
和 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?
你有别的想法吗?我错过了什么?
回答by Mike Thomsen
For versions of jQuery equal or above (>=) 1.6, use:
对于等于或高于 (>=) 1.6 的 jQuery 版本,请使用:
$("#radio_1").prop("checked", true);
For versions prior to (<) 1.6, use:
对于 (<) 1.6 之前的版本,请使用:
$("#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.
提示:您可能还想在之后调用click()
或change()
单选按钮。有关更多信息,请参阅评论。
回答by Vladimir Djuricic
Try this.
尝试这个。
In this example, I'm targeting it with its input name and value
在这个例子中,我用它的输入名称和值来定位它
$("input[name=background][value='some value']").prop("checked",true);
Good to know: in case of multi-word value, it will work because of apostrophes, too.
很高兴知道:在多字值的情况下,它也会因为撇号而起作用。
回答by Umesh Patil
One more function prop() that is added in jQuery 1.6, that serves the same purpose.
在 jQuery 1.6 中添加的另一个函数 prop() 用于相同的目的。
$("#radio_1").prop("checked", true);
回答by Karbaman
Short and easy to read option:
简短易读的选项:
$("#radio_1").is(":checked")
It returns true or false, so you can use it in "if" statement.
它返回 true 或 false,因此您可以在“if”语句中使用它。
回答by Lakshmana Kumar
Try this.
尝试这个。
To check Radio button using Valueuse this.
要使用值检查单选按钮,请使用它。
$('input[name=type][value=2]').attr('checked', true);
Or
或者
$('input[name=type][value=2]').attr('checked', 'checked');
Or
或者
$('input[name=type][value=2]').prop('checked', 'checked');
To check Radio button using IDuse this.
要使用ID检查单选按钮,请使用它。
$('#radio_1').attr('checked','checked');
Or
或者
$('#radio_1').prop('checked','checked');
回答by user4457035
Try This:
尝试这个:
$("input[name=type]").val(['1']);
回答by Amin Saqi
The $.prop
way is better:
该$.prop
方法是更好:
$(document).ready(function () {
$("#radio_1").prop('checked', true);
});
and you can test it like the following:
你可以像下面这样测试它:
$(document).ready(function () {
$("#radio_1, #radio_2", "#radio_3").change(function () {
if ($("#radio_1").is(":checked")) {
$('#div1').show();
}
else if ($("#radio_2").is(":checked")) {
$('#div2').show();
}
else
$('#div3').show();
});
});
回答by JohnP
You have to do
你必须要做
jQuery("#radio_1").attr('checked', 'checked');
That's the HTML attribute
那是 HTML 属性
回答by Anjana Silva
If property name
does not work don't forget that id
still exists. This answer is for people who wants to target the id
here how you do.
如果属性name
不起作用,请不要忘记它id
仍然存在。这个答案适用于想要针对id
您的工作方式的人。
$('input[id=element_id][value=element_value]').prop("checked",true);
Because property name
does not work for me. Make sure you don't surround id
and name
with double/single quotations.
因为财产name
对我不起作用。确保你没有环绕声id
和name
双/单引号。
Cheers!
干杯!
回答by Anjan Kant
Yes, it worked for me like a way:
是的,它对我来说就像这样:
$("#radio_1").attr('checked', 'checked');