gpt4 book ai didi

javascript - 一次仅选择一个复选框

转载 作者:行者123 更新时间:2023-12-02 20:57:11 25 4
gpt4 key购买 nike

我使用引导列表组创建了此列表类型菜单。我想一次只激活一个主要类别。因此,虽然选择了“主类别一”,但现在如果我单击其他类别,“主类别一”将被取消选择,而“主类别二”将被激活。

在提问之前我尝试了一些解决方案,但还没有弄清楚。该列表大约有 15 个主要类别,我只在这里放了两个类别的代码,因为否则代码会更长。希望有人能帮忙。谢谢。

enter image description here

HTML

<a class="list-group-item list-sub-group-item list-group-item-action">
<div class="custom-control custom-checkbox">
<input type="checkbox" value='mcat1' id="mcat1" class="custom-control-input">
<label class="custom-control-label" for="mcat1">Main Category One</label>
</div>
</a>

<div class="list-group-submenu">
<a class="list-group-item list-group-item-action pl-5">
<div class="custom-control custom-checkbox">
<input type="checkbox" value='item1' id="item1" class="custom-control-input">
<label class="custom-control-label" for="item1">Item</label>
</div>
</a>
</div>

<a class="list-group-item list-sub-group-item list-group-item-action">
<div class="custom-control custom-checkbox">
<input type="checkbox" value='mcat2' id="mcat2" class="custom-control-input">
<label class="custom-control-label" for="mcat2">Main Category Two</label>
</div>
</a>

<div class="list-group-submenu">
<a class="list-group-item list-group-item-action pl-5">
<div class="custom-control custom-checkbox">
<input type="checkbox" value='item1' id="item1" class="custom-control-input">
<label class="custom-control-label" for="item1">Item</label>
</div>
</a>
</div>

最佳答案

下面是一个关于如何完成此操作的简单示例。我建议在查找父容器中选中的复选框时更加具体。我刚刚对此示例进行了全局查找。

$('.custom-control-input:checkbox').on('change', function(e){
$('.list-group .custom-control-input').prop('checked', false);
$(this).prop('checked', true);
})
<link href="https://getbootstrap.com/docs/4.4/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="list-group">
<li class="list-group-item">
<a>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck1">
<label class="custom-control-label" for="customCheck1">Check this custom checkbox</label>
</div>
</a>
<ul class="list-group">
<li class="list-group-item">
<a>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck2">
<label class="custom-control-label" for="customCheck2">Check this custom checkbox</label>
</div>
</a>
</li>

</ul>
</li>
<li class="list-group-item">
<a>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck3">
<label class="custom-control-label" for="customCheck3">Check this custom checkbox</label>
</div>
</a>
<ul class="list-group">
<li class="list-group-item">
<a>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck4">
<label class="custom-control-label" for="customCheck4">Check this custom checkbox</label>
</div>
</a>
</li>

</ul>
</li>

</ul>

关于javascript - 一次仅选择一个复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61445665/

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