[英]How to set radio button status with JavaScript
最好使用哪种方法通过 JavaScript 选择性地将单个或多个单选按钮设置为所需的设置?
Very simple很简单
radiobtn = document.getElementById("theid");
radiobtn.checked = true;
the form表格
<form name="teenageMutant">
<input value="aa" type="radio" name="ninjaTurtles"/>
<input value="bb" type="radio" name="ninjaTurtles"/>
<input value="cc" type="radio" name="ninjaTurtles" checked/>
</form>
value="cc" will be checked by default, if you remove the "checked" non of the boxes will be checked when the form is first loaded.默认情况下会选中 value="cc",如果您删除“选中”,则不会在第一次加载表单时选中任何框。
document.teenageMutant.ninjaTurtles[0].checked=true;
now value="aa" is checked.现在检查 value="aa"。 The other radio check boxes are unchecked.
其他单选复选框未选中。
see it in action: http://jsfiddle.net/yaArr/看看它在行动:http: //jsfiddle.net/yaArr/
You may do the same using the form id and the radio button id.您可以使用表单 ID 和单选按钮 ID 执行相同的操作。 Here is a form with id's.
这是一个带有id的表格。
<form id="lizardPeople" name="teenageMutant">
<input id="dinosaurs" value="aa" type="radio" name="ninjaTurtles"/>
<input id="elephant" value="bb" type="radio" name="ninjaTurtles"/>
<input id="dodoBird" value="cc" type="radio" name="ninjaTurtles" checked/>
</form>
value="cc" is checked by default. value="cc" 默认选中。
document.forms["lizardPeople"]["dinosaurs"].checked=true;
now value="aa" with id="dinosaurs" is checked, just like before.现在像以前一样检查 id="dinosaurs" 的 value="aa"。
See it in action: http://jsfiddle.net/jPfXS/在行动中看到它:http: //jsfiddle.net/jPfXS/
Vanilla Javascript:香草Javascript:
yourRadioButton.checked = true;
jQuery: jQuery:
$('input[name=foo]').prop('checked', true);
or或者
$("input:checkbox").val() == "true"
You can also explicitly set value of radio button:您还可以显式设置单选按钮的值:
<form name="gendersForm">
<input type="radio" name="gender" value="M" /> Man
<input type="radio" name="gender" value="F" /> Woman
</form>
with the following script:使用以下脚本:
document.gendersForm.gender.value="F";
and corresponding radio button will be checked automatically.并且相应的单选按钮将被自动选中。
Look at the example on JSFiddle .查看JSFiddle 上的示例。
/**
* setCheckedValueOfRadioButtonGroup
* @param {html input type=radio} vRadioObj
* @param {the radiobutton with this value will be checked} vValue
*/
function setCheckedValueOfRadioButtonGroup(vRadioObj, vValue) {
var radios = document.getElementsByName(vRadioObj.name);
for (var j = 0; j < radios.length; j++) {
if (radios[j].value == vValue) {
radios[j].checked = true;
break;
}
}
}
尝试
myRadio.checked=true
<input type="radio" id="myRadio">My radio<br>
$("#id_of_radiobutton").prop("checked", true);
I am configuring a radio button within a document fragment and tried using radiobtn.checked = true;
我正在文档片段中配置一个单选按钮,并尝试使用
radiobtn.checked = true;
. .
That didn't work so I instead went with this solution:那没有用,所以我改用了这个解决方案:
radiobtn.setAttribute("checked", "checked");
This sets checked using name to cycle through the elements and a value check to set the desired element to true.这将使用名称设置检查以循环遍历元素,并使用值检查将所需元素设置为真。 I kept it as simple as possible, its pretty easy to put it into a function or a loop, etc.
我让它尽可能简单,很容易将它放入函数或循环等中。
variable 'nameValue' is the radio elements name value变量“nameValue”是单选元素名称值
variable 'value' when matched sets the radio button匹配时的变量“值”设置单选按钮
Array.from( document.querySelectorAll('[name="' + nameValue + '"]') ).forEach((element,index) =>
{
if (value === element.value) {
element.checked = true;
} else {
element.checked = false;
}
});
rbtn = document.getElementById("btnid");<br />
rbtn.checked = true;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.