gpt4 book ai didi

javascript - 如果选中,则显示/隐藏 UL 子项

转载 作者:行者123 更新时间:2023-11-28 06:42:46 24 4
gpt4 key购买 nike

我试图在单击父项时显示/隐藏一些 ul 复选框。我到处搜索过,但就是无法让它工作!

有人可以帮我让它工作吗?如果选中父类别,则需要显示/隐藏子复选框。

这是代码:

<div id="refine-categories">
<h4>Categories</h4>
<ul class="parent cf">
<li id='project_category-3' class="popular-category"><label class="selectit"><input value="3" type="checkbox" name="cat_project_category[]" id="in-project_category-3" /> Aanbouw &amp; Opbouw</label><ul class='children'>

<li id='project_category-33'><label class="selectit"><input value="33" type="checkbox" name="cat_project_category[]" id="in-project_category-33" /> aanbouw</label></li>

<li id='project_category-34'><label class="selectit"><input value="34" type="checkbox" name="cat_project_category[]" id="in-project_category-34" /> algemeen</label></li>

<li id='project_category-35'><label class="selectit"><input value="35" type="checkbox" name="cat_project_category[]" id="in-project_category-35" /> Fundering en heiwerken</label></li>

<li id='project_category-36'><label class="selectit"><input value="36" type="checkbox" name="cat_project_category[]" id="in-project_category-36" /> garage bouwen</label></li>

<li id='project_category-37'><label class="selectit"><input value="37" type="checkbox" name="cat_project_category[]" id="in-project_category-37" /> isoleren</label></li>

<li id='project_category-38'><label class="selectit"><input value="38" type="checkbox" name="cat_project_category[]" id="in-project_category-38" /> nieuwbouw</label></li>

<li id='project_category-39'><label class="selectit"><input value="39" type="checkbox" name="cat_project_category[]" id="in-project_category-39" /> opbouw</label></li>

<li id='project_category-40'><label class="selectit"><input value="40" type="checkbox" name="cat_project_category[]" id="in-project_category-40" /> renovatie</label></li>

<li id='project_category-41'><label class="selectit"><input value="41" type="checkbox" name="cat_project_category[]" id="in-project_category-41" /> veranda en serre</label></li>
</li>
</ul> </div>

当我单击父复选框时,我可以看到跨度类更改为 <span class="custom checkbox checked"></span>

希望有人能帮我解决这个问题!

感谢您的宝贵时间!

PS:您可以在这里查看网站:http://bit.ly/20Miaom它位于侧边栏中(右侧)。

最佳答案

.children设置为display:none;

然后从.parent中找到第一个复选框。添加 change 事件,然后添加 toggle .children(如果选中)。

要使所有列表更加通用,请使用 this,返回 .parent 并找到要切换的 .children

$('.parent input[type="checkbox"]:first-child').change(function(){
$(this).closest('.parent').find('.children').toggle($(this).is(':checked'));
});

$('.parent input[type="checkbox"]:first-child').change(function() {
$(this).closest('.parent').find('.children').toggle($(this).is(':checked'));
});
.children {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="refine-categories">
<h4>Categories</h4>
<ul class="parent cf">
<li id='project_category-3' class="popular-category">
<label class="selectit">
<input value="3" type="checkbox" name="cat_project_category[]" id="in-project_category-3" />Aanbouw &amp; Opbouw</label>
<ul class='children'>

<li id='project_category-33'>
<label class="selectit">
<input value="33" type="checkbox" name="cat_project_category[]" id="in-project_category-33" />aanbouw</label>
</li>

<li id='project_category-34'>
<label class="selectit">
<input value="34" type="checkbox" name="cat_project_category[]" id="in-project_category-34" />algemeen</label>
</li>

<li id='project_category-35'>
<label class="selectit">
<input value="35" type="checkbox" name="cat_project_category[]" id="in-project_category-35" />Fundering en heiwerken</label>
</li>

<li id='project_category-36'>
<label class="selectit">
<input value="36" type="checkbox" name="cat_project_category[]" id="in-project_category-36" />garage bouwen</label>
</li>

<li id='project_category-37'>
<label class="selectit">
<input value="37" type="checkbox" name="cat_project_category[]" id="in-project_category-37" />isoleren</label>
</li>

<li id='project_category-38'>
<label class="selectit">
<input value="38" type="checkbox" name="cat_project_category[]" id="in-project_category-38" />nieuwbouw</label>
</li>

<li id='project_category-39'>
<label class="selectit">
<input value="39" type="checkbox" name="cat_project_category[]" id="in-project_category-39" />opbouw</label>
</li>

<li id='project_category-40'>
<label class="selectit">
<input value="40" type="checkbox" name="cat_project_category[]" id="in-project_category-40" />renovatie</label>
</li>

<li id='project_category-41'>
<label class="selectit">
<input value="41" type="checkbox" name="cat_project_category[]" id="in-project_category-41" />veranda en serre</label>
</li>
</li>
</ul>
</div>
<div id="refine-categories">
<h4>Categories 2</h4>
<ul class="parent cf">
<li id='project_category-3' class="popular-category">
<label class="selectit">
<input value="3" type="checkbox" name="cat_project_category[]" id="in-project_category-3" />Aanbouw &amp; Opbouw</label>
<ul class='children'>

<li id='project_category-33'>
<label class="selectit">
<input value="33" type="checkbox" name="cat_project_category[]" id="in-project_category-33" />aanbouw</label>
</li>

<li id='project_category-34'>
<label class="selectit">
<input value="34" type="checkbox" name="cat_project_category[]" id="in-project_category-34" />algemeen</label>
</li>

<li id='project_category-35'>
<label class="selectit">
<input value="35" type="checkbox" name="cat_project_category[]" id="in-project_category-35" />Fundering en heiwerken</label>
</li>

<li id='project_category-36'>
<label class="selectit">
<input value="36" type="checkbox" name="cat_project_category[]" id="in-project_category-36" />garage bouwen</label>
</li>

<li id='project_category-37'>
<label class="selectit">
<input value="37" type="checkbox" name="cat_project_category[]" id="in-project_category-37" />isoleren</label>
</li>

<li id='project_category-38'>
<label class="selectit">
<input value="38" type="checkbox" name="cat_project_category[]" id="in-project_category-38" />nieuwbouw</label>
</li>

<li id='project_category-39'>
<label class="selectit">
<input value="39" type="checkbox" name="cat_project_category[]" id="in-project_category-39" />opbouw</label>
</li>

<li id='project_category-40'>
<label class="selectit">
<input value="40" type="checkbox" name="cat_project_category[]" id="in-project_category-40" />renovatie</label>
</li>

<li id='project_category-41'>
<label class="selectit">
<input value="41" type="checkbox" name="cat_project_category[]" id="in-project_category-41" />veranda en serre</label>
</li>
</li>
</ul>
</div>

关于javascript - 如果选中,则显示/隐藏 UL 子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33652847/

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