Advertisement
Advertisement


How to allow only one radio button to be checked?


Question

{% 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 django template, 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.

2018/12/28
1
146
12/28/2018 11:25:17 AM

Accepted Answer

Simply give them the same name:

<input type="radio" name="radAnswer" />
439
3/24/2011 12:55:43 PM

They need to all have the same name.

2011/03/24

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.

2011/03/24

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" />

2011/03/24

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

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

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

i.e.,

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

2018/12/28

Source: https://stackoverflow.com/questions/5419459
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]