我正在實施一個小腳本,如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