出于某种原因,我似乎无法弄清楚这一点。
我的 html 中有一些单选按钮可以切换类别:
<input type="radio" name="main-categories" id="_1234" value="1234" /> // All
<input type="radio" name="main-categories" id="_2345" value="2345" /> // Certain category
<input type="radio" name="main-categories" id="_3456" value="3456" /> // Certain category
<input type="radio" name="main-categories" id="_4567" value="4567" /> // Certain category
用户可以选择他/她想要的任何一个,但是当某个事件触发时,我想将 1234
设置为选中单选按钮,因为这是默认选中的单选按钮。
我试过这个版本(有和没有 jQuery):
document.getElementById('#_1234').checked = true;
但是好像没有更新。我需要它进行明显更新,以便用户可以看到它。 有人可以帮忙吗?
编辑:我只是累了,忽略了 #
,感谢您指出它和 $.prop()
。
最佳答案
不要将 CSS/JQuery 语法(#
作为标识符)与原生 JS 混合使用。
原生 JS 解决方案:
document.getElementById("_1234").checked = true;
JQuery 解决方案:
$("#_1234").prop("checked", true);
关于javascript - 使用 javascript 检查单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21166860/