Heim > Fragen und Antworten > Hauptteil
P粉7656846022023-09-03 16:34:18
您可以挂钩表格,然后使用它来查找复选框。我使用第一个检查的来设置初始状态,但如果没有,您可能需要调整它。
可能需要更详细一点,只是为了使解决方案更加清晰。
每一条评论都使其始终处于开启状态。如果您希望以编程方式打开/关闭它们,您可以将 'change'
事件更改为 click
let $mainTable = $('#main').find('table tbody'); $mainTable.on('change', 'input[type="checkbox"]', function(event) { let $me = $(this); $me.prop("checked", true); let $parent = $(event.delegateTarget); let checks = $parent.find('input[type="checkbox"]'); checks.not($me).prop("checked", false); checks.not($me).toggleClass('btn-danger off', true); $me.toggleClass("btn-success on", true).toggleClass('btn-danger off', false); // show the classes when checked/unchecked //console.log([...this.classList]); }); // set up the first one as checked to mimic requirement; could be any one $mainTable.find('input[type="checkbox"]') .first(':checked)') .trigger('change'); // now what classes it has we can log those //console.log([...$mainTable.find('input[type="checkbox"]').first(':checked)')[0].classList]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <section id="main" class="content"> <div class="table-responsive"> <table class="table table-striped"> <thead> <tr> <th data-property-name="id" class="sorted integer "> <a href="/admin/?action=list&entity=SchoolYear&sortField=id&sortDirection=ASC&page=1&referer=%252Fadmin%252F%253Faction%253Dlist%2526entity%253DSchoolYear%2526sortField%253Did%2526sortDirection%253DDESC%2526page%253D1"> <i class="fa fa-caret-down"></i> ID </a> </th> <th data-property-name="name" class=" string "> <a href="/admin/?action=list&entity=SchoolYear&sortField=name&sortDirection=DESC&page=1&referer=%252Fadmin%252F%253Faction%253Dlist%2526entity%253DSchoolYear%2526sortField%253Did%2526sortDirection%253DDESC%2526page%253D1"> <i class="fa fa-sort"></i> Name </a> </th> <th data-property-name="start_date" class=" date "> <a href="/admin/?action=list&entity=SchoolYear&sortField=start_date&sortDirection=DESC&page=1&referer=%252Fadmin%252F%253Faction%253Dlist%2526entity%253DSchoolYear%2526sortField%253Did%2526sortDirection%253DDESC%2526page%253D1"> <i class="fa fa-sort"></i> Start date </a> </th> <th data-property-name="end_date" class=" date "> <a href="/admin/?action=list&entity=SchoolYear&sortField=end_date&sortDirection=DESC&page=1&referer=%252Fadmin%252F%253Faction%253Dlist%2526entity%253DSchoolYear%2526sortField%253Did%2526sortDirection%253DDESC%2526page%253D1"> <i class="fa fa-sort"></i> End date </a> </th> <th data-property-name="current" class=" toggle "> <a href="/admin/?action=list&entity=SchoolYear&sortField=current&sortDirection=DESC&page=1&referer=%252Fadmin%252F%253Faction%253Dlist%2526entity%253DSchoolYear%2526sortField%253Did%2526sortDirection%253DDESC%2526page%253D1"> <i class="fa fa-sort"></i> Current </a> </th> <th> <span>Actions</span> </th> </tr> </thead> <tbody> <tr data-id="4"> <td data-label="ID" class="sorted integer "> 4 </td> <td data-label="Name" class=" string "> <span title="SY 2022-2023">SY 2022-2023</span> </td> <td data-label="Start date" class=" date "> <time datetime="2022-07-01T00:00:00-04:00" title="Fri, 01 Jul 2022 00:00:00 -0400">2022-07-01</time> </td> <td data-label="End date" class=" date "> <time datetime="2023-06-30T00:00:00-04:00" title="Fri, 30 Jun 2023 00:00:00 -0400">2023-06-30</time> </td> <td data-label="Current" class=" toggle "> <div class="toggle btn btn-success btn-xs" data-toggle="toggle" style="width: 38px; height: 23px;"><input type="checkbox" checked="" data-toggle="toggle" data-size="mini" data-onstyle="success" data-offstyle="danger" data-on="Yes" data-off="No"> <div class="toggle-group"><label class="btn btn-success btn-xs toggle-on">Yes</label><label class="btn btn-danger btn-xs active toggle-off">No</label><span class="toggle-handle btn btn-default btn-xs"></span></div> </div> </td> <td data-label="Actions" class="actions"> <a href="http://localhost:8000/view-program/4">View</a> <a class="text-primary action-edit" title="" href="/admin/?action=edit&entity=SchoolYear&sortField=id&sortDirection=DESC&page=1&referer=%252Fadmin%252F%253Faction%253Dlist%2526entity%253DSchoolYear%2526sortField%253Did%2526sortDirection%253DDESC%2526page%253D1&id=4" target="_self">Edit</a> <a class="text-danger action-delete" title="" href="/admin/?action=delete&entity=SchoolYear&sortField=id&sortDirection=DESC&page=1&referer=%252Fadmin%252F%253Faction%253Dlist%2526entity%253DSchoolYear%2526sortField%253Did%2526sortDirection%253DDESC%2526page%253D1&id=4" target="_self">Delete</a> </td> </tr> <tr data-id="3"> <td data-label="ID" class="sorted integer "> 3 </td> <td data-label="Name" class=" string "> <span title="SY 2021-2022">SY 2021-2022</span> </td> <td data-label="Start date" class=" date "> <time datetime="2021-07-01T00:00:00-04:00" title="Thu, 01 Jul 2021 00:00:00 -0400">2021-07-01</time> </td> <td data-label="End date" class=" date "> <time datetime="2022-06-30T00:00:00-04:00" title="Thu, 30 Jun 2022 00:00:00 -0400">2022-06-30</time> </td> <td data-label="Current" class=" toggle "> <div class="toggle btn btn-danger off btn-xs" data-toggle="toggle" style="width: 38px; height: 23px;"><input type="checkbox" data-toggle="toggle" data-size="mini" data-onstyle="success" data-offstyle="danger" data-on="Yes" data-off="No"> <div class="toggle-group"><label class="btn btn-success btn-xs toggle-on">Yes</label><label class="btn btn-danger btn-xs active toggle-off">No</label><span class="toggle-handle btn btn-default btn-xs"></span></div> </div> </td> <td data-label="Actions" class="actions"> <a href="http://localhost:8000/view-program/3">View</a> <a class="text-primary action-edit" title="" href="/admin/?action=edit&entity=SchoolYear&sortField=id&sortDirection=DESC&page=1&referer=%252Fadmin%252F%253Faction%253Dlist%2526entity%253DSchoolYear%2526sortField%253Did%2526sortDirection%253DDESC%2526page%253D1&id=3" target="_self">Edit</a> <a class="text-danger action-delete" title="" href="/admin/?action=delete&entity=SchoolYear&sortField=id&sortDirection=DESC&page=1&referer=%252Fadmin%252F%253Faction%253Dlist%2526entity%253DSchoolYear%2526sortField%253Did%2526sortDirection%253DDESC%2526page%253D1&id=3" target="_self">Delete</a> </td> </tr> <tr data-id="2"> <td data-label="ID" class="sorted integer "> 2 </td> <td data-label="Name" class=" string "> <span title="SY 2019-2020">SY 2019-2020</span> </td> <td data-label="Start date" class=" date "> <time datetime="2019-07-01T00:00:00-04:00" title="Mon, 01 Jul 2019 00:00:00 -0400">2019-07-01</time> </td> <td data-label="End date" class=" date "> <time datetime="2020-06-30T00:00:00-04:00" title="Tue, 30 Jun 2020 00:00:00 -0400">2020-06-30</time> </td> <td data-label="Current" class=" toggle "> <div class="toggle btn btn-danger off btn-xs" data-toggle="toggle" style="width: 38px; height: 23px;"><input type="checkbox" data-toggle="toggle" data-size="mini" data-onstyle="success" data-offstyle="danger" data-on="Yes" data-off="No"> <div class="toggle-group"><label class="btn btn-success btn-xs toggle-on">Yes</label><label class="btn btn-danger btn-xs active toggle-off">No</label><span class="toggle-handle btn btn-default btn-xs"></span></div> </div> </td> <td data-label="Actions" class="actions"> <a href="http://localhost:8000/view-program/2">View</a> <a class="text-primary action-edit" title="" href="/admin/?action=edit&entity=SchoolYear&sortField=id&sortDirection=DESC&page=1&referer=%252Fadmin%252F%253Faction%253Dlist%2526entity%253DSchoolYear%2526sortField%253Did%2526sortDirection%253DDESC%2526page%253D1&id=2" target="_self">Edit</a> <a class="text-danger action-delete" title="" href="/admin/?action=delete&entity=SchoolYear&sortField=id&sortDirection=DESC&page=1&referer=%252Fadmin%252F%253Faction%253Dlist%2526entity%253DSchoolYear%2526sortField%253Did%2526sortDirection%253DDESC%2526page%253D1&id=2" target="_self">Delete</a> </td> </tr> <tr data-id="1"> <td data-label="ID" class="sorted integer "> 1 </td> <td data-label="Name" class=" string "> <span title="SY 2020-2021">SY 2020-2021</span> </td> <td data-label="Start date" class=" date "> <time datetime="2020-07-01T00:00:00-04:00" title="Wed, 01 Jul 2020 00:00:00 -0400">2020-07-01</time> </td> <td data-label="End date" class=" date "> <time datetime="2021-06-30T00:00:00-04:00" title="Wed, 30 Jun 2021 00:00:00 -0400">2021-06-30</time> </td> <td data-label="Current" class=" toggle "> <div class="toggle btn btn-xs btn-danger off" data-toggle="toggle" style="width: 38px; height: 23px;"><input type="checkbox" data-toggle="toggle" data-size="mini" data-onstyle="success" data-offstyle="danger" data-on="Yes" data-off="No" checked="checked"> <div class="toggle-group"><label class="btn btn-success btn-xs toggle-on">Yes</label><label class="btn btn-danger btn-xs active toggle-off">No</label><span class="toggle-handle btn btn-default btn-xs"></span></div> </div> </td> <td data-label="Actions" class="actions"> <a href="http://localhost:8000/view-program/1">View</a> <a class="text-primary action-edit" title="" href="/admin/?action=edit&entity=SchoolYear&sortField=id&sortDirection=DESC&page=1&referer=%252Fadmin%252F%253Faction%253Dlist%2526entity%253DSchoolYear%2526sortField%253Did%2526sortDirection%253DDESC%2526page%253D1&id=1" target="_self">Edit</a> <a class="text-danger action-delete" title="" href="/admin/?action=delete&entity=SchoolYear&sortField=id&sortDirection=DESC&page=1&referer=%252Fadmin%252F%253Faction%253Dlist%2526entity%253DSchoolYear%2526sortField%253Did%2526sortDirection%253DDESC%2526page%253D1&id=1" target="_self">Delete</a> </td> </tr> </tbody> </table> </div> </section>
P粉3114649352023-09-03 11:19:13
您可以在更改事件处理程序中操作复选框的类。这不是选中/取消选中复选框的方式。
请阅读此使用 jquery 选中/取消选中复选框? 和这个 jQuery。如何取消选中除一个(已选中)之外的所有复选框
以下是具体操作方法:
$('#main').find('table .toggle input[type="checkbox"]').change(function() { $('table .toggle input[type="checkbox"]').prop('checked', false); $(this).prop('checked', true); });
这是工作示例:https://jsfiddle.net/fL9dknp7/