开发者

Selecting and deselecting multiple checkboxes

开发者 https://www.devze.com 2023-01-14 18:10 出处:网络
I have this: $(\'input.people\').attr(\'checked\', function() { return $.inArray(this.name, [\'danB\', \'lindaC\']) != -1;

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 thi开发者_如何学Gonking 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');
});
0

精彩评论

暂无评论...
验证码 换一张
取 消