2011-12-01 107 views
0

我有一些使用属性rel分组的复选框。当任何人不被检查时,我想检查是否所有人都没有检查,然后做一些事情。Jquery检查匹配的元素列表

场景:我有一个父级复选框,即所有的孩子都未选中,父级应该是。如果选中一个或多个,则需要检查父项。

所有儿童复选框的类名都是child

$('.child').change(function() { 
    var groupdId = $(this).attr('rel'); 
    if($('.child').where('rel', groupId).any('checked', true)) <-- how do I do this? 
    { 
     //Uncheck the parent 
    } 
    else { 
     //Check the parent. 
    } 
}); 

我试图在这里是:使用类.child和Rel == groupdId元素,如果其中任何检查,做X。

回答

0

未经检验的,但我认为这会工作。

var $boxes = $('input.child'); 

$boxes.click(function() { 
    var $box = $(this), 
     grp = $box.attr('rel'), 
     $boxes_in_group = $boxes.filter("[rel='"+ grp +"']"); 

    if ($box[0].checked) 
     // Check the parent. 
    else if ($boxes_in_group.filter(":checked").length == 0) 
     // Uncheck the parent. 
});