2010-09-05 139 views
1

我有這樣的:選擇和取消選擇多個複選框

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

這是聯繫人列表。我希望能夠讓用戶點擊複選框,選擇特定角色中的人員並取消選中其他人。這樣可行。現在我想對此進行修改,以便如果有人點擊相同的複選框以取消選擇這些人(並且僅限那些人)。這將是一個onBlur事件?

我想是這樣的:

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

還是會:

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

我將如何整合這與頂級功能?順便提一下,尼克·克拉弗對他的幫助迄今爲止一直在大聲呼喊。

回答

0
$('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

在這裏看到爲什麼這個作品和討論什麼其他方法的缺點是:http://stackoverflow.com/questions/355638/jquery-toggle-event-is-messing-with-checkbox-值 – 2010-09-05 19:50:19