【发布时间】:2020-05-15 12:01:22
【问题描述】:
<!--
if all checkboxes are not 'checkecd' then check all
if some are 'checked' then check all
if all are 'checked' then uncheck all
-->
const btn = document.querySelector('button');
btn.addEventListener('click',()=>{
const allboxes = document.querySelectorAll('input[type="checkbox"]');
allboxes.forEach(box => {
if(!box.checked){
box.checked = true;
} else {
box.checked = false;
}
})
})
<button>select all</button>
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
【问题讨论】:
-
这两个意思相同
if all checkboxes are not 'checkecd' then check all if some are 'checked' then check all -
点击“全选”按钮时,如果所有复选框都“未选中”,则检查所有复选框是否已选中,有些未选中,然后检查所有复选框是否已选中,然后取消选中所有复选框跨度>
-
这能回答你的问题吗? Check all checkboxes with JavaScript
标签: javascript input checkbox checkboxlist