简体   繁体   中英

Selecting and deselecting multiple checkboxes

I have this:

$('input.people').attr('checked', function() {
  return $.inArray(this.name, ['danB', 'lindaC']) != -1;
});

It is for a list of contacts. I want to be able to have the user click a checkbox that will select the people in a specific role and uncheck everyone else. That works. Now I want to modify this so that if someone unclicks the same checkbox that it deselects those people (and only those people). Would that be an onBlur event?

I'm thinking something like this:

$('input.people').attr('checked', function(){
   return $.inArray(this.name, ['danB', 'lindaC']) != 1;
});

or would it be:

$('input.people').attr('checked', function(){
   return $.inArray(this.name, ['danB', 'lindaC']) = -1;
});

And how would I integrate this with the top function? A shout out, btw, to Nick Craver for his help to date.

$('input.people').change(function ()
{
    if(!$(this).hasClass("checked"))
    {
        //do stuff if the checkbox is checked
        $(this).addClass("checked");
        return;
    }

    //do stuff if the checkbox isn't checked
    $(this).removeClass('checked');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM