我的jsfiddle在这里与复选框一起工作。
我的问题是:当我定期检查所有孩子的复选框时,未检查父母,并且不会褪色。
$(document).ready(function() {
$(".pc-box").click(function() {
if (this.checked) {
$(this).closest("li").find(".cc-box").prop("checked", true);
$(this).parent().fadeOut();
}
});
$(".cc-box").click(function() {
if (!this.checked)
$(this).closest("ul").prev().fadeIn().find(".pc-box").prop("checked", false);
});
});
您必须使用parents()
而不是parent()
。parent()
用于靶向直接父,而parents('#selector')
用于针对链中的父,除非找到选择器
使用此代码:
$(".pc-box").click(function() {
if (this.checked) {
$(this).parents("li").find(".cc-box").prop("checked", true);
$(this).parents('li').fadeOut();
}
});
demo