【问题标题】:2 sets of radio buttons with same IDs2组具有相同ID的单选按钮
【发布时间】:2015-02-03 15:07:54
【问题描述】:

是否可以有 2 组共享一个 ID 的单选按钮?我似乎无法让他们好好相处。

演示:http://abenjamin765.github.io/slidemenu/

<input type="radio" name="set-one" id="radio-one">
<input type="radio" name="set-one" id="radio-two">
<input type="radio" name="set-one" id="radio-three">

<input type="radio" name="set-two" id="radio-one">
<input type="radio" name="set-two" id="radio-two">
<input type="radio" name="set-two" id="radio-three">

干杯!

【问题讨论】:

  • ID 必须是唯一的。
  • 现在:也许您可以解释为什么您希望元素共享一个唯一标识符?我怀疑你试图解决的问题比这条路线更容易解决。
  • ID 必须是唯一的。如果需要,请尝试为它们分配相同的类,并为该类对输入执行操作。
  • 我将单选按钮用作导航栏中的选项卡,而不是单选选项。它需要足够灵活,以便在页面上多次出现而不会发生冲突。

标签: html forms input


【解决方案1】:

没有。 ID 属性必须是唯一的。 ID 属性表示该单个元素的唯一标识。

来自HTML5 specification's id attribute section

3.2.5.1 id属性

id 属性指定其元素的唯一标识符 (ID)。

该值在元素的主子树中的所有 ID 中必须是唯一的,并且必须包含至少一个字符。

如果您想让他们共享一个引用(例如,出于样式目的),您可以改用 class 属性:

<input type="radio" name="set-one" class="radio-one">
<input type="radio" name="set-one" class="radio-two">
<input type="radio" name="set-one" class="radio-three">

<input type="radio" name="set-two" class="radio-one">
<input type="radio" name="set-two" class="radio-two">
<input type="radio" name="set-two" class="radio-three">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-25
    • 2014-10-26
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 2021-05-11
    • 2014-03-14
    • 1970-01-01
    相关资源
    最近更新 更多