我正在实施一个小脚本,如http://css-tricks.com/examples/IndeterminateCheckboxes/ 但我的html包含一个<a>
标记作为复选框的父级,所以这导致了问题。我不知道如何解决它。选择具有不确定功能的多个复选框
<ul>
<li"><a href="#"><input type="checkbox" >1 </a>
<ul>
<li><a href="#"><input type="checkbox">1.1</a></li>
</ul>
</li>
<li><a href="#"><input type="checkbox" >2 </a>
<ul>
<li><a href="#"><input type="checkbox" >2.1</a>
<ul>
<li><a href="#"><input type="checkbox" >2.1.1</a></li>
<li><a href="#"><input type="checkbox" >2.1.2</a></li>
<li><a href="#"><input type="checkbox">2.1.3</a></li>
</ul>
</li>
</ul>
</li>
</ul>
我的剧本是
<script>
$(function() {
// Apparently click is better chan change? Cuz IE?
$('input[type="checkbox"]').change(function(e) {
var checked = $(this).prop("checked"),
container = $(this).parent().parent(),
siblings = container.siblings();
container.find('input[type="checkbox"]').prop({
indeterminate: false,
checked: checked
});
function checkSiblings(el) {
var parent = el.parent(),
all = true;
console.log(parent);
el.siblings().each(function() {
all = ($(this).children('input[type="checkbox"]').prop("checked") === checked);
console.log(all);
return all;
});
if (all && checked) {
parent.children('input[type="checkbox"]').prop({
indeterminate: false,
checked: checked
});
checkSiblings(parent);
} else if (all && !checked) {
parent.children('input[type="checkbox"]').prop("checked", checked);
parent.children('input[type="checkbox"]').prop("indeterminate", (parent.find('input[type="checkbox"]:checked').length > 0));
//console.log("thi sis ");
checkSiblings(parent);
} else {
el.parents("li").children('input[type="checkbox"]').prop({
indeterminate: true,
checked: false
});
console.log("apple");
}
}
checkSiblings(container);
});
});
</script>
我已经另一个脚本连接到它所以标签是必要的任何进一步。解决方案受到欢迎 – Sajan