用一个开关切换所有开关
Toggle all switches with one switch
我有以下 table.
我想在切换第一个开关 class 名称 checkbox-input-main
时切换所有名称 class 名称 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>
您错过了 $(".the-checkbox-input").attr("checked",true)
中的 ,true
但这更简单
我使用 PROP,因为这是推荐的,并且在取消选中一个时使用 attr 似乎不会重新检查
$(function() {
const $mainCheck = $(".checkbox-input-main");
const $chks = $(".the-checkbox-input");
$mainCheck.on("click", function() {
$chks.prop("checked", this.checked);
});
$chks.on("click", function() {
const checked = $chks.filter(function() { return this.checked }).length
$mainCheck.prop("checked", checked === $chks.length)
});
});
<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>
纯 JS
window.addEventListener("load", function() {
const mainCheck = document.querySelector(".checkbox-input-main");
const chks = [...document.querySelectorAll(".the-checkbox-input")]; /// cast to array
mainCheck.addEventListener("click", function() {
chks.forEach(chk => chk.checked = this.checked);
});
chks.forEach(chk => chk.addEventListener("click", function() {
const checked = chks.filter(chk => chk.checked).length;
mainCheck.checked = checked === chks.length;
})
);
});
<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>
我有以下 table.
我想在切换第一个开关 class 名称 checkbox-input-main
时切换所有名称 class 名称 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>
您错过了 $(".the-checkbox-input").attr("checked",true)
中的 ,true
但这更简单
我使用 PROP,因为这是推荐的,并且在取消选中一个时使用 attr 似乎不会重新检查
$(function() {
const $mainCheck = $(".checkbox-input-main");
const $chks = $(".the-checkbox-input");
$mainCheck.on("click", function() {
$chks.prop("checked", this.checked);
});
$chks.on("click", function() {
const checked = $chks.filter(function() { return this.checked }).length
$mainCheck.prop("checked", checked === $chks.length)
});
});
<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>
纯 JS
window.addEventListener("load", function() {
const mainCheck = document.querySelector(".checkbox-input-main");
const chks = [...document.querySelectorAll(".the-checkbox-input")]; /// cast to array
mainCheck.addEventListener("click", function() {
chks.forEach(chk => chk.checked = this.checked);
});
chks.forEach(chk => chk.addEventListener("click", function() {
const checked = chks.filter(chk => chk.checked).length;
mainCheck.checked = checked === chks.length;
})
);
});
<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>