如果未選中所有子項,我想取消選中父節點。如果在JQuery中未選中所有子項,則取消選中父節點
<ul id="treeList">
<li>
<input type="checkbox" name="selectedRole">
Application Manager
<ul>
<li>
<input type="checkbox" name="selectedRole">
Application Manager Panel
</li>
<li>
<input type="checkbox" name="selectedRole">
Client Role
<ul>
<li>
<input type="checkbox" name="selectedRole">
Client Task 1
</li>
<li>
<input type="checkbox" name="selectedRole">
Client Task 2
</li>
</ul>
</li>
</ul>
</li>
我有以下jQuery腳本。但是當我沒有選中任何孩子時,父母沒有選中。它應該看到,如果所有的孩子都沒有檢查。那麼它應該取消選中父項。
jQuery.each(jQuery('#treeList ul li').find(':checkbox'), function(){
jQuery(this).change(function(){
if (jQuery(this).is(':checked')) {
jQuery(this).parents('ul').siblings('input:checkbox').attr('checked', true);
}else{
jQuery(this).parents('ul').siblings('input:checkbox').attr('checked', false);
}
});
});
對不起,它不起作用。如果我有另一個節點,如客戶端角色,並且它還可以有2個客戶端任務。有任何提示嗎?因爲它不適用於客戶端角色的兄弟節點。 –
@Tahir Akram對代碼做了一個小小的更新,應該按照預期工作。 – Niklas
A)。檢查客戶端任務2(最後一個節點) B)。取消選中客戶端任務2。它將取消選中客戶端角色。但是應用程序管理器(頂層節點)將保持檢查狀態。 [問題] –