当我选中帐户设置复选框时,所有子项li输入标记都会完美检查。但是,当我取消选中子级li复选框添加和删除时,它也会取消选中帐户设置。
当 AS One 和 AS Two 被选中时,我不想取消选中帐户设置。
帮助我,如何解决这个问题。
此处Jsfiidle
下面是我的代码。
HTML
<ul class="tree" id="tree">
<li>
<input type="checkbox" name="account_settings" value="yes">Account Settings
<!-- AND SHOULD CHECK HERE -->
<ul>
<li>
<input type="checkbox" name="one" value="one">AS One</li>
<li>
<input type="checkbox" name="two" value="two">AS Two</li>
<li>
<input type="checkbox" name="user_roles" value="user_roles">Users & Roles
<!-- SHOULD CHECK HERE -->
<ul>
<li>
<input type="checkbox" name="user_role" value="add">Add</li>
<li>
<input type="checkbox" name="user_role" value="delete">Delete</li>
<!-- CHECK HERE -->
</ul>
</li>
</ul>
</li>
<li>
<input type="checkbox" name="rl_module" value="yes">RL Module</li>
<li>
<input type="checkbox" name="rl_module" value="yes">Accounting
<ul>
<li>
<input type="checkbox" name="vat" value="yes">VAT</li>
<li>
<input type="checkbox" name="bank_account" value="yes">Banking
<ul>
<li>
<input type="checkbox" name="view" value="yes">View</li>
<li>
<input type="checkbox" name="crud" value="yes">CRUD</li>
</ul>
</li>
</ul>
</li>
</ul>
JS
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
$('input[type=checkbox]').click(function () {
$(this).parent().find('li input[type=checkbox]').prop('checked', $(this).is(':checked'));
var sibs = false;
$(this).closest('ul').children('li').each(function () {
if($('input[type=checkbox]', this).is(':checked')) sibs=true;
})
$(this).parents('ul').prev().prop('checked', sibs);
});
答案 0 :(得分:2)
试试这个
<ul class="tree" id="tree">
<li>
<input type="checkbox" name="account_settings" value="yes">Account Settings
<ul>
<li>
<input class="account_settings" type="checkbox" name="one" value="one">AS One
</li>
<li>
<input class="account_settings" type="checkbox" name="two" value="two">AS Two
</li>
<li>
<input class="account_settings" type="checkbox" name="user_roles" value="user_roles">Users & Roles
<ul>
<li>
<input class="account_settings user_roles" type="checkbox" name="user_role" value="add">Add
</li>
<li>
<input class="account_settings user_roles" type="checkbox" name="user_role" value="delete">Delete
</li>
</ul>
</li>
</ul>
</li>
</ul>
$('input[type=checkbox]').click(function () {
if($(this).prop('checked'))
{
$('.'+$(this).attr('name')).prop('checked',true);
}
else
{
$('.'+$(this).attr('name')).prop('checked',false);
}
findparent(this);
});
});
function findparent(elem)
{
var flag = 1;
var fcount = 0;
var count = 0;
$(elem).parent().parent().find('li input').each( function ( index ) {
fcount = fcount +1;
if($(this).prop('checked') == false)
count = count + 1;
});
if(count == fcount)
flag = 0;
if(flag == 0)
$(elem).parent().parent().prev().prop('checked',false);
else
$(elem).parent().parent().prev().prop('checked',true);
}