话不多说,直接上代码。
HTML:
<p>
<label><input type="checkbox" name="selFruit" id="selFruit" οnclick="selAll()"/>水果全选:</label>
<input type="checkbox" name="fruit" value="apple" οnclick="getSel()">苹果
<input type="checkbox" name="fruit" value="banana" οnclick="getSel()">香蕉
<input type="checkbox" name="fruit" value="orange" οnclick="getSel()">橘子
</p>
JS:
<script>
//全选与全不选
function selAll(){
let fruit = document.frm.fruit
if(document.frm.selFruit.checked){
for(let i=0;i<fruit.length;i++){
fruit[i].checked = true
}
}