如果我有一组单选按钮,其中一个被默认设置为选中:
<input type="radio" id="id1" name="grp" value="val1" checked="checked">
<input type="radio" id="id2" name="grp" value="val2">
<input type="radio" id="id3" name="grp" value="val3">
如果我现在获取 ID 为“id2”的元素并执行
var el = ...;
el.checked = true;
其他元素的 checked
属性会自动设置为 false
吗?
链接到描述该行为的官方文档的奖励积分。
最佳答案
Does setting the checked attribute on a radio button change the checked attributed on other buttons with the same name?
是的,确实如此。我很惊讶地发现 DOM2 HTML specification 中没有明确记录此行为.但是,它记录在 HTML5 specification 中和 HTML 4.01 specification .
关于javascript - 在单选按钮上设置选中属性是否会更改其他同名按钮上的选中属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5935083/