Jquery check two checkboxes remove checkboxes with checkboxes checked

I have a form with two sets of flags. The first is a list of roles in the committee (chairman, deputy chairman, secretary, etc.). The second set of flags is a list of people in these roles. For example, Todd is our vice chairman, and Gwen is our treasurer. If I want Todd's checkbox selected when I click on the Vice Chair checkbox, I can do this:

$('input[name=toddS]').attr('checked', true);

But if I want to select our treasuer, I want him to not only set the Gwen checkbox, but also uncheck Todd (and all the other selected checkboxes). How can I do this so that no matter what people check, it leaves only the person or people in this role are checked and leave all the remaining flags unchecked. It is clear? I hope so.

Scott

+3
source share
2 answers

Instead, use the radio buttons, this way only one radio button will be checked, and the others automatically. Not even jQuery needed. Just make sure you call the switches the same with a different value, for example:

<input type="radio" name="position" value="Chairman" /> Chairman
<input type="radio" name="position" value="Vice Chairman" />Vice  Chairman
<input type="radio" name="position" value="Secretory" /> Secretory
<!-- and so on -->

Similarly, for people:

<input type="radio" name="person" value="Todd" /> Todd
<input type="radio" name="person" value="Gwen" />Vice  Gwen
<input type="radio" name="person" value="Micheal" /> Micheal
<!-- and so on -->
+1
source

If the list of names looks like this:

<input type="checkbox" name="person" value="Todd" />Todd
<input type="checkbox" name="person" value="Gwen" />Gwen
<input type="checkbox" name="person" value="Micheal" />Micheal

you can use the following js to clear all selections

$('input[name=person]').attr('checked', false);

A little late answer that will help you, but might help someone else ...

0
source

Source: https://habr.com/ru/post/1759707/


All Articles