【发布时间】:2014-03-22 02:54:03
【问题描述】:
我想在单击任何子项时选择父项。此代码正在检查和检查。
$(function() {
$(".child").on("click",function() {
$parent = $(this).prevAll(".parent");
if ($(this).is(":checked")) $parent.prop("checked",true);
else {
var len = $(this).parent().find(".child:checked").length;
$parent.prop("checked",len>0);
}
});
$(".parent").on("click",function() {
$(this).parent().find(".child").prop("checked",this.checked);
});
});
它与输入一起工作(见link)
<input type='checkbox' name='cat' class='parent' value='cat1' />Category 1<br>
<input type='checkbox' name='foo' class='child' value='1' />SubCategory 1<br>
<input type='checkbox' name='foo' class='child' value='1' />SubCategory 2
但使用
<input type='checkbox' name='cat' class='parent' value='cat1' />Category 1
<ul>
<il><input type='checkbox' name='foo' class='child' value='1' />SubCategory 1</il>
<il><input type='checkbox' name='foo' class='child' value='1' />SubCategory 2</il>
</ul>
【问题讨论】:
-
如果你想得到答案,你应该真正展示你的代码,链接你的小提琴,并更好地解释你的问题。
-
查看链接了解问题
标签: javascript