[英]jquery selecting/unselecting checkboxes in children/parent <li>
I have the following 我有以下内容
<ul>
<li>Main
<ul>
<li><input type="checkbox" onclick="$.SelectChildren(this);" /> parent 1
<ul>
<li><input type="checkbox" onclick="$.SelectChildren(this);" />sub 1</li>
<li><input type="checkbox" onclick="$.SelectChildren(this);" />sub 2
<ul>
<li><input type="checkbox" onclick="$.SelectChildren(this);" />sub sub 2</li>
<li><input type="checkbox" onclick="$.SelectChildren(this);" />sub sub 3</li>
</ul>
</li>
</ul>
</li>
<li><input type="checkbox" onclick="$.SelectChildren(this);" />parent 2</li>
</ul>
</li>
</ul>
Now for my JQuery function: 现在为我的JQuery函数:
(function ($) {
$.SelectChildren = function (element) {
if ($(element).is(':checked')) {
$(element).parent().find(':checkbox').attr('checked', true);
}
else {
$(element).parent().find(':checkbox').attr('checked', false);
}
}
})(jQuery);
how would I unselect ONLY (all)parent checkboxes when un-checking a child 取消选中儿童时,如何取消选择“仅(所有)父”复选框
for example: when clicked "sub sub 2" checkbox -> it should uncheck "sub 2" and "parent 1" checkboxes as well. 例如:单击“ sub sub 2”复选框->它也应取消选中“ sub 2”和“ parent 1”复选框。
thanks 谢谢
Use .children()
(to get immediate children) instead of .find()
(which looks everywhere below) on the .parents()
, for example: 例如,在.parents()
上使用.parents()
.children()
(以获得直接的孩子),而不是.find()
(在下面到处都是)。
(function ($) {
$.SelectChildren = function (element) {
$(element).parents().children(':checkbox').attr('checked', element.checked);
};
})(jQuery);
Since you're passing a boolean, you can just use the .checked
property directly as well. 由于要传递布尔值,因此也可以直接直接使用.checked
属性。
You can give it a try here , the more traditional approach to get the same effect would look like this: 您可以在这里尝试一下 ,获得相同效果的更传统的方法如下所示:
$(function() {
$(":checkbox").change(function () {
$(this).parents().children(':checkbox').attr('checked', this.checked);
});
});
You can use this code to allow a checkbox to show/hide childer elements OR elements withint a DIV (or in this case a bootstrap group). 您可以使用此代码来允许复选框显示/隐藏DIV(或在这种情况下是引导程序组)内的子元素或元素。
Step #1 - apply the class 'switch' to the checkbox Make sure the checkbox id is set 步骤#1-将“开关”类应用于复选框确保已设置复选框ID
Step #2 - for the element you want to hide/toggle add the class with chcekbox ID 步骤#2-为要隐藏/切换的元素添加带有chcekbox ID的类
// -------------------------
// Checkbox to toggle related input fields
// Checkbox control class = switch
// -------------------------
$('.switch').each(function() {
var tItems = $(this).attr('id');
$('.' + tItems).hide();
});
// Toggle elements
// <div class="control-group applied_before <-- checkbox ID">
$('.switch').change(function () {
var tItems = $(this).attr('id');
$('.' + tItems).toggle();
});
// -------------------------
// -------------------------
<fieldset>
<legend>Criminal History</legend>
<div class="control-group">
<input type="checkbox" name="criminal_offender" id="criminal_offender" class="switch" />
<label>criminal_offender</label>
</div>
<div class="control-group criminal_offender">
<label>criminal_offense</label>
<input type="text" name="criminal_offense" id="criminal_offense" />
</div>
<div class="control-group criminal_offender">
<label>criminal_st_code</label>
<input type="text" name="criminal_st_code" id="criminal_st_code" />
</div>
<div class="control-group criminal_offender">
<label>criminal_date</label>
<input type="text" name="criminal_date" id="criminal_date" />
</div>
</fieldset>
<script>
var checkParent = function(ref) {
return $(ref).parent().parent().parent().children().first();
}
$('input[type="checkbox"]').click(function(){
if(this.checked){
$(this).parent().children().find('input[type="checkbox"]').attr('checked', true);
}
else {
var a = checkParent(this);
while(a.length > 0 && a.prop("tagName") == "INPUT" )
{
a.attr("checked", false);
a = checkParent(a);
}
}
});
</script>
Just completed this algorithm for a project and thought i'll share it on stack flow 刚刚为一个项目完成了该算法,并认为我会在堆栈流中共享它
Basically, if you check a parent, all the children are selected and when you uncheck a child, all the parents associated with it will be unnchecked because a Parent can remain checked only if all the children are checked 基本上,如果选中父级,则将选中所有子级,并且取消选中子级时,将取消选中与之关联的所有父级,因为只有选中所有子级后,父级才能保持选中状态
Make sure all the checkboxes are within li and all the li are within ul otherwise this won't work 确保所有复选框都在li内,所有li都在ul内,否则将无法正常工作
sample html 样本HTML
<ul id="a">
<ul id="b">
<li id="parentli"><input type="checkbox" id="parent"> Parent
<ul id="c">
<li><input type="checkbox" id="aa">Child 1</li>
<li><input type="checkbox" id="ab">Child 2
<ul id="d">
<li><input type="checkbox" id="aba">Child 2-1</li>
</ul>
</li>
<li><input type="checkbox" id="ac">Child 3</li>
</ul>
</li>
</ul>
</ul>
How about: 怎么样:
$(element).parents(":checkbox").attr('checked', false);
UPDATE. UPDATE。 This is wrong, see Nick's solution. 这是错误的,请参阅尼克的解决方案。
Identify the parent will a specific class like "parent" 确定父母会像“父母”这样的特定班级
Then 然后
$(".parent").attr("checked", false);
Very Simple 很简单
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.