出于某种原因,我似乎无法弄清楚这一点。
我的 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()
。
原文由 ptf 发布,翻译遵循 CC BY-SA 4.0 许可协议
不要将 CSS/JQuery 语法(
#
用于标识符)与本机 JS 混合。原生 JS 解决方案:
document.getElementById("_1234").checked = true;
jQuery解决方案:
$("#_1234").prop("checked", true);