【发布时间】:2021-12-01 17:24:35
【问题描述】:
我有下表。
当切换第一个类名checkbox-input-main 的开关时,我想切换类名the-checkbox-input 的所有开关。我尝试了以下代码,但它不起作用。代码对我来说似乎很好。
我该如何解决这个问题?我尝试了很多,但找不到任何错误。代码还是不行。
$(document).ready(function() {
$(".checkbox-input-main").on("click", function() {
if ($(this).is(":checked")) {
$(".the-checkbox-input").attr("checked");
} else {
$(".the-checkbox-input").removeAttr("checked");
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<table class="table table-bordered text-center dso-table">
<thead class="thead-dark">
<tr>
<th scope="col">
<label class="switch mb-0">
<input type="checkbox" class="checkbox-input-main">
<span class="slider round"></span>
</label>
</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">
<label class="switch">
<input type="checkbox" class="the-checkbox-input">
<span class="slider round"></span>
</label>
</th>
</tr>
<tr>
<th scope="row">
<label class="switch">
<input type="checkbox" class="the-checkbox-input">
<span class="slider round"></span>
</label>
</th>
</tr>
</tbody>
</table>
【问题讨论】:
-
我给你加了一个sn-p并加了您只需将值
true添加到checked属性:$(".the-checkbox-input").attr("checked", true);
标签: javascript jquery checkbox toggle uiswitch