checkbox结构:
<ol>
<input class="level0" type="checkbox" />
<li>
<input class="level1" type="checkbox" /></li>
<li>
<input class="level1" type="checkbox" /></li>
<li>
<input class="level1" type="checkbox" /></li>
<li>
<input class="level1" type="checkbox" /></li>
<li>
<input class="level1" type="checkbox" /></li>
<li>
<input class="level1" type="checkbox" /></li>
<li>
<ol>
<input class="level2" type="checkbox" />
<li>
<input class="level3" type="checkbox" /></li>
<li>
<input class="level3" type="checkbox" /></li>
<li>
<input class="level3" type="checkbox" /></li>
<li>
<input class="level3" type="checkbox" /></li>
</ol>
</li>
</ol>
js代码:
$(".level0").click(function() {
$(":checkbox").each(function() {
$(this).attr("checked", $(".level0").attr("checked"));
});
});
$(".level2").click(function() {
$("li > ol :checkbox").each(function() {
$(this).attr("checked", $(".level2").attr("checked"));
});
});
$(":checkbox").not($(":checkbox")[0]).click(function() {
var ch = $(this).attr("checked");
if (ch == false)
$(".level0").attr("checked", false);
});
$("li > ol li :checkbox").click(function() {
var ch = $(this).attr("checked");
if (ch == false)
$(".level2").attr("checked", false);
});
本文介绍了一种使用JavaScript实现的复选框级联选择功能,即当父级复选框被选中时,其下所有子级复选框也会被选中;反之亦然。此外还介绍了如何在子级复选框状态改变时更新父级复选框的状态。
891

被折叠的 条评论
为什么被折叠?



