202
{% 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.

Clinteney Hui
  • 4,385
  • 5
  • 24
  • 21

8 Answers8

594

Simply give them the same name:

<input type="radio" name="radAnswer" />
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • 1
    OMG... well, if I dont assign names to them. they all should have empty string as name by default right? Thank you – Clinteney Hui Mar 24 '11 at 12:58
  • 27
    @Clinteney without a name they're not really part of the form and their value won't be sent when submitting the form. The name is used by the browser to set "groups" of radio buttons, only one radio button in each group can be selected at one time and selecting other will clear the previous selection. :) – Shadow The GPT Wizard Mar 24 '11 at 13:02
  • For the project I just inherited, both radio buttons on my form MUST be named differently. One field is an ALL field, the other field is dynamic, based on a value in the URL. They want the user to only be able to select one radio. – user3120861 Jul 24 '20 at 17:06
  • @user3120861 so the only way is by using client side code, i.e. JavaScript. It's possible with plain JS, but easier with jQuery. For more details, please hit me up on chat, and if we'll get something I'll add to the answer. Cheers! :) – Shadow The GPT Wizard Jul 24 '20 at 17:38
70

They need to all have the same name.

Douglas
  • 36,802
  • 9
  • 76
  • 89
52

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.

Jack M.
  • 30,350
  • 7
  • 55
  • 67
Nick
  • 1,904
  • 1
  • 17
  • 34
25

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

FluxEngine
  • 12,730
  • 14
  • 57
  • 83
19

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.

SuKu
  • 383
  • 3
  • 11
11

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

<form><input type="radio" name="selection">
      <input type="radio" name="selection">
      ..
      ..
</form>
CaptJak
  • 3,592
  • 1
  • 29
  • 50
VinayKumar.M
  • 147
  • 1
  • 4
5

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

B.K
  • 847
  • 10
  • 6
1

Only one Step you need to follow!

Make Sure you need to add the attribute name in the opening tag with the same values of that attribute name!

Example :

<input name="18+" value="yes" id="18" type="radio">Yes
  <input name="18+" value="No" id="bel" type="radio">No
Pavan Tanniru
  • 176
  • 1
  • 2
  • 10