Javascript 如何只选中和取消选中所有特定的手风琴?
这里我有一个手风琴的动态列表。手风琴里面有复选框。如果单击“全部选中”,我想选中仅特定手风琴的所有复选框。现在,如果单击“全部”,则选中“所有手风琴”中的复选框 html 您可以使用Javascript 如何只选中和取消选中所有特定的手风琴?,javascript,html,jquery,Javascript,Html,Jquery,这里我有一个手风琴的动态列表。手风琴里面有复选框。如果单击“全部选中”,我想选中仅特定手风琴的所有复选框。现在,如果单击“全部”,则选中“所有手风琴”中的复选框 html 您可以使用$(this).closest(“.collapse”).find(“:checkbox”)查找该div中的所有复选框并使其选中/取消选中 演示代码: $('.all')。单击(函数(事件){ var this=this //获取最近的collpse类并在其中找到所有复选框 变量选择器=$(this).closes
$(this).closest(“.collapse”).find(“:checkbox”)
查找该div中的所有复选框并使其选中/取消选中
演示代码:
$('.all')。单击(函数(事件){
var this=this
//获取最近的collpse类并在其中找到所有复选框
变量选择器=$(this).closest(“.collapse”).find(“:复选框”).not(this)
选择器。每个(函数(){
this.checked=this_u.checked?真:假;
});
});代码>
全部检查
一张支票
两张支票
全部检查
一张支票
两张支票
{% for u in users %}
<div>
<a data-toggle="collapse" data-target="#collapse{{u.id}}" aria-expanded="false" aria-controls="collapse">
{{u.name}}</a>
</div>
<div id="collapse{{u.id}}" class="collapse" aria-labelledby="one" data-parent="#accordion">
<div class="col-3">
<div class="my-checkbox">
<input type="checkbox" name='all' class="all" id="check{{u.id}}">
<label class="custom-control-label" for="check{{u.id}}">Check All</label>
</div>
</div>
<div class="col-3">
<div class="my-checkbox">
<input type="checkbox" name='all' class="all" id="check{{u.id}}">
<label class="custom-control-label" for="check{{u.id}}">One check</label>
</div>
</div>
<div class="col-3">
<div class="my-checkbox">
<input type="checkbox" name='all' class="all" id="check{{u.id}}">
<label class="custom-control-label" for="check{{u.id}}">Two check</label>
</div>
</div>
</div>
{% endfor %}
$('.all').click(function(event) {
if(this.checked) {
$(':checkbox').each(function() {
this.checked = true;
});
} else {
$(':checkbox').each(function() {
this.checked = false;
});
}
});