在选中第一个复选框时选中第二个复选框,可以通过以下几种方式实现:
// HTML
<input type="checkbox" id="checkbox1">
<input type="checkbox" id="checkbox2">
// JavaScript
const checkbox1 = document.getElementById('checkbox1');
const checkbox2 = document.getElementById('checkbox2');
checkbox1.addEventListener('change', function() {
checkbox2.checked = checkbox1.checked;
});
// HTML
<input type="checkbox" id="checkbox1">
<input type="checkbox" id="checkbox2">
// JavaScript with jQuery
$('#checkbox1').change(function() {
$('#checkbox2').prop('checked', $(this).prop('checked'));
});
<input type="checkbox" id="checkbox1" onchange="checkSecondCheckbox()">
<input type="checkbox" id="checkbox2">
<script>
function checkSecondCheckbox() {
const checkbox1 = document.getElementById('checkbox1');
const checkbox2 = document.getElementById('checkbox2');
checkbox2.checked = checkbox1.checked;
}
</script>
以上是几种常见的实现方式,具体选择哪种方式取决于你的项目需求和技术栈。
领取专属 10元无门槛券
手把手带您无忧上云