jQuery 设置单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9525128/
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
jQuery set radio button
提问by mike628
I am trying to set a radio button. I want set it by using the value or the id.
我正在尝试设置一个单选按钮。我想通过使用值或 id 来设置它。
This is what I've tried.
这是我试过的。
$('input:radio[name=cols]'+" #"+newcol).attr('checked',true);
newcol
is the id of the radio button.
newcol
是单选按钮的 ID。
Maybe a little edit is in order.
也许需要稍作修改。
There are two sets of radio boxes one with cols and the other with rows. So I see the point in not using id's. My bad. So I have as an example:
有两组单选框,一组带有 cols,另一组带有 row。所以我看到了不使用 id 的意义。我的错。所以我举个例子:
<input type="radio" name="rows" class="listOfCols"
style="width: 50%; " value="Site"></input>
and
和
<input type="radio" name="cols" class="listOfCols"
style="width: 50%; " value="Site"></input>
with the id's removed, and I need to set the correct one.
删除了 ID,我需要设置正确的 ID。
回答by Jasper
Your selector looks for the descendant of a input:radio[name=cols]
element that has the id of newcol
(well the value of that variable).
您的选择器会查找input:radio[name=cols]
id 为newcol
(以及该变量的值)的元素的后代。
Try this instead (since you're selecting by ID anyway):
试试这个(因为你无论如何都是按 ID 选择的):
$('#' + newcol).prop('checked',true);
Here is a demo: http://jsfiddle.net/jasper/n8CdM/1/
这是一个演示:http: //jsfiddle.net/jasper/n8CdM/1/
Also, as of jQuery 1.6 the perferred method of altering a property is .prop()
: http://api.jquery.com/prop
此外,从 jQuery 1.6 开始,更改属性的首选方法是.prop()
:http: //api.jquery.com/prop
回答by Chococroc
I found the answer here:
https://web.archive.org/web/20160421163524/http://vijayt.com/Post/Set-RadioButton-value-using-jQuery
我在这里找到了答案:https: //web.archive.org/web/20160421163524/http:
//vijayt.com/Post/Set-RadioButton-value-using-jQuery
Basically, if you want to check one radio button, you MUST pass the value as an array:
基本上,如果您想检查一个单选按钮,您必须将值作为数组传递:
$('input:radio[name=cols]').val(['Site']);
$('input:radio[name=rows]').val(['Site']);
回答by Darin Dimitrov
In your selector you seem to be attempting to fetch some nested element of your radio button with a given id. If you want to check a radio button, you should select this radio button in the selector and not something else:
在您的选择器中,您似乎试图使用给定的 id 获取单选按钮的某些嵌套元素。如果你想检查一个单选按钮,你应该在选择器中选择这个单选按钮而不是别的:
$('input:radio[name="cols"]').attr('checked', 'checked');
This assumes that you have the following radio button in your markup:
这假设您的标记中有以下单选按钮:
<input type="radio" name="cols" value="1" />
If your radio button had an id:
如果您的单选按钮有一个 id:
<input type="radio" name="cols" value="1" id="myradio" />
you could directly use an id selector:
您可以直接使用 id 选择器:
$('#myradio').attr('checked', 'checked');
回答by punita.gosar
You can try the following code:
您可以尝试以下代码:
$("input[name=cols][value=" + value + "]").attr('checked', 'checked');
This will set the attribute checked for the radio columns and value as specified.
这将设置为指定的单选列和值检查的属性。
回答by elclanrs
Why do you need 'input:radio[name=cols]'
. Don't know your html, but assuming that ids are unique, you can simply do this.
你为什么需要'input:radio[name=cols]'
. 不知道您的 html,但假设 ids 是唯一的,您可以简单地执行此操作。
$('#'+newcol).prop('checked', true);
回答by The Awnry Bear
Try this:
尝试这个:
$("#" + newcol).attr("checked", "checked");
I've had issues with attr("checked", true)
, so I tend to use the above instead.
我遇到了问题attr("checked", true)
,所以我倾向于使用上面的。
Also, if you have the ID then you don't need that other stuff for selection. An ID is unique.
此外,如果您有 ID,那么您就不需要其他东西进行选择。ID 是唯一的。
回答by dhc
回答by Graham Laight
Combining previous answers:
结合以前的答案:
$('input[name="cols"]').filter("[value='Site']").click();
回答by Selvakumar Arumugam
Since newcol
is the ID of the radio button, You can simply use it as below.
由于newcol
是单选按钮的 ID,您可以简单地使用它,如下所示。
$("#"+newcol).attr('checked',true);
回答by Markoj
You can simply use:
您可以简单地使用:
$("input[name='cols']").click();