【发布时间】:2019-11-21 17:03:00
【问题描述】:
我正在尝试实现全选复选框,它工作正常,唯一的问题是当我取消选中复选框时,全选功能停止对该特定复选框工作。
请注意,我使用的是 Laravel 框架,所以不要对 php 代码做出反应。
$(document).ready(function() {
$("#BulkSelect").click(function() {
$('.Bulkers').attr('checked', this.checked);
});
$('.Bulkers').change(function() {
if ($(".Bulkers").length == $(".Bulkers:checked").length) {
$("#BulkSelect").attr("checked", "checked");
} else {
$("#BulkSelect").removeAttr("checked");
}
});
});
<table id="data-table" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th><input type="checkbox" id="BulkSelect"></th>
<th>Hater's Name</th>
<th>Hater's Profile Link</th>
<th>Victim's Name</th>
<th>Victim's Post Link</th>
<th>Country</th>
<th>Source</th>
<th>
<div class="btn-group" role="group">
<button id="btnGroupDrop3" type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Action
</button>
<div class="dropdown-menu" aria-labelledby="btnGroupDrop3" x-placement="bottom-start">
<form method="post" id="BulkDelete" action="{{my_route}}">
@csrf
<button type="submit" class="dropdown-item">Delete</button>
<button type="button" onclick="BulkApprove()" class="dropdown-item">Approve</button>
</form>
</div>
</div>
</th>
</tr>
</thead>
<tbody>
@php($i = 0) @foreach($tables as $table)
<tr class="gradeX">
<td><input type="checkbox" name="ids[]" form="BulkDelete" class="Bulkers" value="{{$table->id}}"></td>
<td>{{$table->column_name}}</td>
<td>{{$table->column_name}}</td>
<td>{{$table->column_name}}</td>
<td>{{$table->column_name}}</td>
<td>{{$table->column_name}}</td>
<td>{{$table->column_name}}</td>
<td>
<div class="btn-group" role="group">
<button id="btnGroupDrop3" type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Action
</button>
</div>
</td>
</tr>
@endforeach
</tbody>
</table>
【问题讨论】:
-
你用的是什么版本的jQuery?
-
另一种可能的方法是为您的 selectAll 复选框提供一个数据属性,该属性在选中和取消选中该框时会发生变化,您可以根据每个复选框的数据属性执行一些
if-else语句 -
jquery 3.4.1 @PeterKA
-
嗨@MaazKhan,删除您的php代码并将您的输出代码粘贴为html
-
我会使用
.prop("checked", true/false)而不是.attr("checked", "....")
标签: javascript jquery html checkbox selectall