javascript - 在单选按钮上设置选中属性是否会更改其他同名按钮上​​的选中属性?

标签 javascript

如果我有一组单选按钮,其中一个被默认设置为选中:

<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 .

Gratuitous live example

关于javascript - 在单选按钮上设置选中属性是否会更改其他同名按钮上​​的选中属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5935083/

相关文章:

javascript - 通过分页按键迭代 Firestore 集合中的所有项目

javascript - 如何将 angular-ui modal templateURL 与 Node/快速路由一起使用

javascript - Ember 组件功能

javascript - EmberJS View - 检测是否所有 subview 都已呈现

javascript - 形成两个 html 字段之间的关系

javascript - Google 跟踪代码管理器 - 捕获选择值

javascript - 您可以使用 JavaScript 将 CSS 背景图像变成提交按钮吗?

javascript - 折叠/展开表列(不是行)

javascript - React 不渲染第二个嵌套元素

javascript - 如何将数据传回父窗口?