jQuery 使用值设置从无线电组中选择的无线电
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4618733/
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
Set selected radio from radio group with a value
提问by dcolumbus
Why am I struggling with this?
我为什么要为此而苦苦挣扎?
I have a value: 5
我有一个值:5
How do I check the radio button of group "mygroup" with the value of 5?
如何检查值为 5 的“mygroup”组的单选按钮?
$("input[name=mygroup]").val(5); // doesn't work?
回答by Felix Kling
With the help of the attribute selectoryou can select the input element with the corresponding value. Then you have to set the attribute explicitly, using .attr
:
在属性选择器的帮助下,您可以选择具有相应值的输入元素。然后你必须显式设置属性,使用.attr
:
var value = 5;
$("input[name=mygroup][value=" + value + "]").attr('checked', 'checked');
Since jQuery 1.6, you can also use the .prop
method with a boolean value (this should be the preferred method):
从 jQuery 1.6 开始,您还可以使用.prop
带有布尔值的方法(这应该是首选方法):
$("input[name=mygroup][value=" + value + "]").prop('checked', true);
Remember you first need to remove checked attribute from any of radio buttons under one radio buttons group only then you will be able to add checked property / attribute to one of the radio button in that radio buttons group.
请记住,您首先需要从一个单选按钮组下的任何单选按钮中删除选中的属性,然后才能将选中的属性/属性添加到该单选按钮组中的一个单选按钮。
Code To Remove Checked Attribute from all radio buttons of one radio button group -
从一个单选按钮组的所有单选按钮中删除选中属性的代码 -
$('[name="radioSelectionName"]').removeAttr('checked');
回答by Jonathan Pasquier
There is a better way of checking radios and checkbox; you have to pass an array of valuesto the val method instead of a raw value
有一种更好的方法来检查收音机和复选框;您必须将一组值而不是原始值传递给 val 方法
Note:If you simply pass the value by itself (without being inside an array), that will result in all values of "mygroup" being set to the value.
注意:如果您只是单独传递值(而不是在数组内),这将导致“mygroup”的所有值都设置为该值。
$("input[name=mygroup]").val([5]);
Here is the jQuery doc that explains how it works: http://api.jquery.com/val/#val-value
这是解释它如何工作的 jQuery 文档:http: //api.jquery.com/val/#val-value
And .val([...])
also works with form elements like <input type="checkbox">
, <input type="radio">
, and <option>
s inside of a <select>
.
而且.val([...])
还与表单元素<input type="checkbox">
,<input type="radio">
和<option>
S的一个内<select>
。
The inputs and the options having a value that matches one of the elements of the arraywill be checked or selected, while those having a value that don't match one of the elements of the array will be unchecked or unselected
具有与数组元素之一匹配的值的输入和选项将被选中或选中,而那些具有与数组元素之一不匹配的值的输入和选项将被取消选中或未选中
Fiddle demonstrating this working: https://jsfiddle.net/92nekvp3/
小提琴演示此工作:https: //jsfiddle.net/92nekvp3/
回答by David says reinstate Monica
Try this:
尝试这个:
$('input:radio[name="mygroup"][value="5"]').attr('checked',true);
回答by hunter
$("input[name='mygroup'][value='5']").attr("checked", true);
回答by Jean-Marc Amon
When you change attribute value like mentioned above the change
event is not triggered so if needed for some reasons you can trigger it like so
当您像上面提到的那样更改属性值时,change
不会触发事件,因此如果出于某些原因需要,您可以像这样触发它
$('input[name=video_radio][value="' + r.data.video_radio + '"]')
.prop('checked', true)
.trigger('change');
回答by karan
$("input[name='RadioTest'][value='2']").prop('checked', true);
$("input[name='RadioTest'][value='2']").prop('checked', true);
回答by Ketan
Or you can just write value attribute to it:
或者您可以只向其写入 value 属性:
$(':radio[value=<yourvalue>]').attr('checked',true);
This works for me.
这对我有用。
回答by Amin Fathi
var key = "Name_radio";
var val = "value_radio";
var rdo = $('*[name="' + key + '"]');
if (rdo.attr('type') == "radio") {
$.each(rdo, function (keyT, valT){
if ((valT.value == $.trim(val)) && ($.trim(val) != '') && ($.trim(val) != null))
{
$('*[name="' + key + '"][value="' + (val) + '"]').prop('checked', true);
}
})
}
回答by JukkaV
Pure JavaScript version:
纯 JavaScript 版本:
document.querySelector('input[name="myradio"][value="5"]').checked = true;
回答by Dharmendra Patel
$("input[name=mygroup]").val([5]);
Here is the jQuery doc that explains how it works: http://api.jquery.com/val/#val-value
这是解释它如何工作的 jQuery 文档:http: //api.jquery.com/val/#val-value