Html 如何只允许选中一个单选按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5419459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:33:26  来源:igfitidea点击:

How to allow only one radio button to be checked?

htmldjangoforms

提问by Clinteney Hui

{% for each in AnswerQuery %}
    <form action={{address}}>
        <span>{{each.answer}}</span><input type='radio'>
        <span>Votes:{{each.answercount}}</span>
        <br>
    </form>
{% endfor %}

This is a part my djangotemplate, what it supposed to do is to print out several radio buttons, corresponding to the answers assigned to the buttons. But I don't know why I can check multiple radio buttons, which messed me up. It is supposed to only let me check on one radio button and I had that somehow but I lost it. Any help? Thank you.

这是我的django模板的一部分,它应该做的是打印出几个单选按钮,对应于分配给按钮的答案。但我不知道为什么我可以检查多个单选按钮,这让我很困惑。它应该只让我检查一个单选按钮,我以某种方式拥有它,但我失去了它。有什么帮助吗?谢谢你。

回答by Shadow Wizard is Ear For You

Simply give them the same name:

只需给它们相同的名称:

<input type="radio" name="radAnswer" />

回答by Douglas

They need to all have the same name.

它们都需要具有相同的名称。

回答by Nick

All radio buttons have to have the same name:

所有单选按钮必须具有相同的名称:

<input type='radio' name='foo'>

Only 1 radio button of each group of buttons with the same name can be checked.

每组同名按钮中只能勾选 1 个单选按钮。

回答by FluxEngine

Give them the same name, and it will work. By definition Radio buttons will only have one choice, while check boxes can have many.

给它们起相同的名字,它会起作用。根据定义,单选按钮只有一种选择,而复选框可以有多种选择。

<input type="radio" name="Radio1" />

<input type="radio" name="Radio1" />

回答by VinayKumar.M

Just give them the same name throughout the form you are using.

只需在您使用的整个表单中为它们指定相同的名称。

<form><input type="radio" name="selection">
      <input type="radio" name="selection">
      ..
      ..
</form>

回答by B.K

All the radio buttons options must have the same name for you to be able to select one option at a time.

所有单选按钮选项必须具有相同的名称,以便您能够一次选择一个选项。

回答by SuKu

Add "name" attribute and keep the name same for all the radio buttons in a form.

添加“名称”属性并保持表单中所有单选按钮的名称相同。

i.e.,

IE,

<input type="radio" name="test" value="value1"> Value 1
<input type="radio" name="test" value="value2"> Value 2
<input type="radio" name="test" value="value3"> Value 3

Hope that would help.

希望这会有所帮助。