[英]Check/uncheck parents and children
Can you do a better code?你能做一个更好的代码吗? I need to check/uncheck all childs according to parent and when an child is checked, check parent, when all childs are unchecked uncheck parent.
我需要根据父母检查/取消选中所有子项,并且当检查一个孩子时,检查父母,当所有子女均未选中,取消选中父母。
$(".parent").children("input").click(function() {
$(this).parent().siblings("input").attr("checked", this.checked);
});
$(".parent").siblings("input").click(function() {
if (this.checked) {
$(this).siblings("div").children("input").attr("checked", true);
return;
}
var childs = $(this).siblings("div").siblings("input");
for (i = 0; i < childs.length; i++) {
if ($(childs.get(i)).attr("checked"))
return;
}
$(this).parent().children("div").children("input").attr("checked", false);
});
$(".parent").children("input").click(function() {
$(this).parent().siblings("input").attr("checked", this.checked);
});
$(".parent").siblings("input").click(function() {
$(this).siblings("div").children("input").attr("checked",
this.checked || $(this).siblings("input[checked]").length>0
);
});
woah, i'm mega confused.哇,我超级困惑。 it looks as though you have inputs with other inputs inside of them?
看起来好像你有输入,里面有其他输入? ...which doesn't make sense.
……没有意义。 Here's what I think your structure looks like, so here I go.
这就是我认为你的结构的样子,所以我走了。
<div class="parent">
<input type="checkbox" />
<div>
<input type="checkbox" />
<input type="checkbox" />
</div>
<input type="checkbox" />
<div>
<input type="checkbox" />
<input type="checkbox" />
</div>
</div>
And here's the code I'd use.这是我要使用的代码。
$("input[type='checkbox']").click(function() {
// turn on or off all descendants.
$(this) // get this checkbox
// get the div directly after it
.next('div')
// get ALL the inputs in that div (not just first level children)
.find("input[type='checkbox']")
.attr("checked", this.checked)
;
// now check if we have to turn the parent on or off.
$(this)
.parent() // this will be the div
.prev('input[type="checkbox"]') // this is the input
.attr(
"checked", // set checked to true if...
this.checked // this one is checked, or...
|| $(this).siblings("input[type='checkbox'][checked]").length > 0
// any of the siblings are checked.
)
;
});
update: i've just tested this and it totally works (woo!).更新:我刚刚测试了这个,它完全有效(哇!)。 It also works with as many levels of nesting as you want, not just two.
它还可以根据需要处理尽可能多的嵌套级别,而不仅仅是两个级别。
$('.parent').click(function(){
checkBox = $(this).find('input[type=checkbox]');
checkBox.prop("checked", !checkBox.prop("checked")); // inverse selection
});
Ultimate.最终的。
This is a lovely little thread that has got me nearly where I need to be.这是一个可爱的小线程,它让我几乎到达了我需要去的地方。 I've slightly adapted Nickf's code.
我稍微修改了 Nickf 的代码。 The aim is that checking a child would also check the parent, and unchecking a parent would also uncheck all children.
目的是检查子项也会检查父项,取消选中父项也会取消选中所有子项。
$("input[type='checkbox']").click(function() { if (!$(this.checked)) { $("input[type='checkbox']").click(function() { if (!$(this.checked)) {
$(this)
.next('div')
.find("input[type='checkbox']")
.attr("checked", this.checked)
;
}
$(this)
.parent()
.prev('input[type="checkbox"]')
.attr("checked", this.checked || $(this).siblings("input[type='checkbox'][checked]").length > 0
)
;
}); });
How would one go about tweaking this so that all descendants are unchecked if the parent is unchecked?如果父级未选中,如何调整它以便所有后代都未选中?
New all things JQ and javascript..apologies.新的 JQ 和 javascript 的所有东西......道歉。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.