gpt4 book ai didi

javascript - 如何取消父复选框

转载 作者:太空宇宙 更新时间:2023-11-04 10:39:12 25 4
gpt4 key购买 nike

我的页面上有嵌套的复选框,如果我点击父复选框,子复选框被选中,那么到目前为止没问题。但是如果我点击子复选框,我希望父点击必须被取消选中怎么办?

html

<html lang="en">
<head>
<meta charset="UTF-8">
<title>No Title</title>
</head>
<body>
<div class="new-checkbox">
<ul>
<li>
<input type="checkbox" id="input1"><label for="input1">kategori 1</label>
<ul>
<li><input type="checkbox" id="input11"><label for="input11">kategori 11</label></li>
<li><input type="checkbox" id="input12"><label for="input12">kategori 12</label></li>
<li><input type="checkbox" id="input13"><label for="input13">kategori 13</label></li>
</ul>
</li>
<li><input type="checkbox" id="input2"><label for="input2">kategori 2</label></li>
<li>
<input type="checkbox" id="input3"><label for="input3">kategori 3</label>
<ul>
<li><input type="checkbox" id="input31"><label for="input31">kategori 31</label></li>
<li><input type="checkbox" id="input32"><label for="input32">kategori 32</label></li>
<li>
<input type="checkbox" id="input33"><label for="input33">kategori 33</label>
<ul>
<li><input type="checkbox" id="input331"><label for="input331">kategori 331</label></li>
<li><input type="checkbox" id="input332"><label for="input332">kategori 332</label></li>
<li><input type="checkbox" id="input333"><label for="input333">kategori 333</label></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<!--new-checkbox-->
<script type="text/javascript" src="https://cdn.anitur.com.tr/js/jquery-1.8.2.min.js" ></script>
</body>
</html>

CSS

.new-checkbox ul {
padding: 0;
margin: 0;
list-style: none;
margin-left: 30px;
font: normal 11px/16px"Segoe UI", Arial, Sans-serif;
}
.new-checkbox ul:first-child {
margin-left: 0;
}
.new-checkbox ul li {
margin: 3px 0;
}
.new-checkbox input[type="checkbox"] {
display: none;
}
.new-checkbox label {
cursor: pointer;
}
.new-checkbox input[type="checkbox"] + label:before {
border: 1px solid #ffffff;
content: "\00a0";
display: inline-block;
font: 16px/1em sans-serif;
height: 13px;
width: 13px;
margin: 2px .25em 0 0;
padding: 0;
vertical-align: top;
border: solid 1px #1375b3;
color: #1375b3;
opacity: .50;
}
.new-checkbox input[type="checkbox"]:checked + label:before {
background: #fff;
color: #1375b3;
content: "\2714";
text-align: center;
box-shadow: 0 0 2px rgba(0, 0, 0, .25) inset;
opacity: 1;
}
.new-checkbox input[type="checkbox"]:checked + label:after {
font-weight: bold;
}

j查询

$(document).ready(function() {
$('.new-checkbox input[type=checkbox]').on("click", function() {
if ($(this).is(':checked')) {
// check all children
$(this).parent().find('li input[type=checkbox]').prop('checked', true);
// check all parents
$(this).parent().prev().prop('checked', true);
} else {
// uncheck all children
$(this).parent().find('li input[type=checkbox]').prop('checked', false);
}
});
});

关于 codepen

最佳答案

添加一个新的检查来查找被检查的元素是否有父元素然后检查是否所有的子元素都被点击了。此外,最好使用 change 事件。

$(document).ready(function() {
$('.new-checkbox input[type=checkbox]').on("change", function() {
var $close = $(this).closest('ul').closest('li');
if ($(this).is(':checked')) {
// check all children
$(this).parent().find('li input[type=checkbox]').prop('checked', true);
// check all parents
$(this).parent().prev().prop('checked', true);
} else {
// uncheck all children
$(this).parent().find('li input[type=checkbox]').prop('checked', false);
}
while ($close.length) {
$che = $close.find('ul input:checkbox');
$checked = $close.find('ul input:checkbox:checked');
$close.children('input').prop('checked', $che.length == $checked.length);
$close = $($close.children('input')).closest('ul').closest('li');
console.log($che.length, $checked.length);
}
});
});
.new-checkbox ul {
padding: 0;
margin: 0;
list-style: none;
margin-left: 30px;
font: normal 11px/16px"Segoe UI", Arial, Sans-serif;
}
.new-checkbox ul:first-child {
margin-left: 0;
}
.new-checkbox ul li {
margin: 3px 0;
}
.new-checkbox input[type="checkbox"] {
display: none;
}
.new-checkbox label {
cursor: pointer;
}
.new-checkbox input[type="checkbox"] + label:before {
border: 1px solid #ffffff;
content: "\00a0";
display: inline-block;
font: 16px/1em sans-serif;
height: 13px;
width: 13px;
margin: 2px .25em 0 0;
padding: 0;
vertical-align: top;
border: solid 1px #1375b3;
color: #1375b3;
opacity: .50;
}
.new-checkbox input[type="checkbox"]:checked + label:before {
background: #fff;
color: #1375b3;
content: "\2714";
text-align: center;
box-shadow: 0 0 2px rgba(0, 0, 0, .25) inset;
opacity: 1;
}
.new-checkbox input[type="checkbox"]:checked + label:after {
font-weight: bold;
}
<script type="text/javascript" src="https://cdn.anitur.com.tr/js/jquery-1.8.2.min.js"></script>
<div class="new-checkbox">
<ul>
<li>
<input type="checkbox" id="input1">
<label for="input1">kategori 1</label>
<ul>
<li>
<input type="checkbox" id="input11">
<label for="input11">kategori 11</label>
</li>
<li>
<input type="checkbox" id="input12">
<label for="input12">kategori 12</label>
</li>
<li>
<input type="checkbox" id="input13">
<label for="input13">kategori 13</label>
</li>
</ul>
</li>
<li>
<input type="checkbox" id="input2">
<label for="input2">kategori 2</label>
</li>
<li>
<input type="checkbox" id="input3">
<label for="input3">kategori 3</label>
<ul>
<li>
<input type="checkbox" id="input31">
<label for="input31">kategori 31</label>
</li>
<li>
<input type="checkbox" id="input32">
<label for="input32">kategori 32</label>
</li>
<li>
<input type="checkbox" id="input33">
<label for="input33">kategori 33</label>
<ul>
<li>
<input type="checkbox" id="input331">
<label for="input331">kategori 331</label>
</li>
<li>
<input type="checkbox" id="input332">
<label for="input332">kategori 332</label>
</li>
<li>
<input type="checkbox" id="input333">
<label for="input333">kategori 333</label>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>

关于javascript - 如何取消父复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35962983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com