我需要动态选择一个单选按钮。
i) 我动态地创建了几个单选按钮,即在一个函数中使用:
function funcName()
{
Something.innerHTML = '<input type = "radio" name = "rName" id = "1" />' + '<input type = "radio" name = "rName" id = "2" />' + '<input type = "radio" name = "rName" id = "3" />';
}
ii) 我需要在另一个函数中动态选择一个单选按钮:
function funcNAme(someVar)
{
document.getElementById(someVar).checked = true;
}
但这不起作用,即单选按钮未被选中。但是,“禁用”工作正常。 还有:
alert(someVar);
返回适当的期望值。
最佳答案
语法如下:
element.checked = true;
所以你需要使用
document.getElementById(someVar).checked = true;
请看这里 -> HTMLInputElement
工作示例 -> http://jsfiddle.net/p5yca/
请不要使用数字作为元素的 ID -> http://www.w3.org/TR/html401/types.html#type-name
关于javascript - 动态选择单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8357917/