为什么我可以检查多个单选按钮?

t3a*_*3ax 5 html forms radio-button

我有一个带有单选按钮的 HTML 表单并且可以选择多个但为什么呢?我无法自拔。

这是我的 HTML:

<input type="radio" name="nameA" id="nameA" value="nameA">
<label for="nameA">Choice A</label>
<input type="radio" name="nameB" id="nameB" value="nameB">
<label for="nameB">Choice B</label>
Run Code Online (Sandbox Code Playgroud)

对于发现此问题的任何人:解决方案是给他们相同的名称

<input type="radio" name="sameName" id="nameA" value="nameA">
<label for="nameA">Choice A</label>
<input type="radio" name="sameName" id="nameB" value="nameB">
<label for="nameB">Choice B</label>
Run Code Online (Sandbox Code Playgroud)

Que*_*tin 6

所有具有相同名称并且是相同形式的控件的单选按钮都是一个组的一部分。

一组中只能选中一个单选按钮。

您有两个名称不同的单选按钮。这意味着您有两个单选组,每个组包含一个单选按钮。

如果您只想选择其中一个,则需要将它们放在同一个组中(通过让它们共享一个名称)。

(它们仍然应该有唯一的 id(这样你可以给每个人一个标签)和值(这是你在表单提交到服务器时确定哪个被检查的方式))。

<form>
  <fieldset>
    <legend>Thing that is being chosen</legend>

    <input type="radio" name="name" id="nameA" value="nameA">
    <label for="nameA">Choice A</label>

    <input type="radio" name="name" id="nameB" value="nameB">
    <label for="nameB">Choice B</label>

  </fieldset>
</form>
Run Code Online (Sandbox Code Playgroud)