dimanche 2 avril 2017

Check only 1 checkbox based on id

So I am building a website which will include a questionnaire section. Actually processing the answers and checking they are correct is all up and working, however, users are still able to select multiple answers (checkboxes) when they should only be able to check one.

I essentially want to be able to only allow users to check one checkbox for each question, and as can be seen in the code below, the ids for each answer are uniquely identified, i.e. 1-0, 1-1, 2-3, 2-4.

I want to be able to only allow one checkbox with an id prefixed with 1- or 2- etc, to be checked. So the below code would only allow either 1-0 or 1-1 to be checked, and either 2-3 or 2-4 to be checked.

<form action='...' method='post'>
                <p id='0'>Question 1</p>
                <label><input type='checkbox' id='1-0' name='answers[]' value='1'>Answer 1</label>
                <label><input type='checkbox' id='1-1' name='answers[]' value='2'>Answer 2</label>

                <p id='1'>Question 2</p>
                <label><input type='checkbox' id='2-3' name='answers[]' value='1'>Answer 1</label>
                <label><input type='checkbox' id='2-4' name='answers[]' value='2'>Answer 2</label>

            <input type="submit" name="submit" value="Submit"/>
    </form>

Any help would be greatly appreciated.




Aucun commentaire:

Enregistrer un commentaire