HTML代码
<input type="checkbox" class="chk" name="" id="" value=""/> <input type="checkbox" class="chk" name="" id="" value=""/> <input type="checkbox" class="chk" name="" id="" value=""/> <input type="checkbox" class="chk" name="" id="" value=""/> <input type="checkbox" class="chk" name="" id="" value=""/> <input type="checkbox" class="chk" name="" id="" value=""/> <input type="checkbox" class="chk" name="" id="" value=""/> <button onclick="fanxuan()">反选</button> <button onclick="quanxuan()">全选</button> <button onclick="quanbuxuan()">全不选</button>
script代码
function fanxuan(){ var chk = document.getElementsByClassName("chk"); for (var i=0;i<chk.length;i++ ) { if(chk[i].checked){ chk[i].checked = false; }else{ chk[i].checked = true; } } } function quanxuan() { var aa = document.getElementsByClassName('chk'); for(var i=0; i<aa.length; i++){ if(aa[i].name==name){ aa[i].checked = true; } } } function quanbuxuan() { var bb = document.getElementsByClassName('chk'); for(var i=0; i<bb.length; i++){ if(bb[i].name==name){ bb[i].checked = false; } } }
反选示例
全选
全不选