本文主要是介绍js实现全选和全不选,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
非常简洁的几行原生js实现全选和全不选,大神们可以在此基础上加上反选功能啊^^
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><title>全选、全不选</title>
</head>
<body>
<table border="1"><tr><td><input type="checkbox" name="mmAll" onclick="All(this, 'mm[]')"></td></tr><tr><td><input type="checkbox" value="1" name="mm[]" class="check" id="ids1" onclick="Item(this, 'mmAll')"></td></tr><tr><td><input type="checkbox" value="2" name="mm[]" class="check" id="ids2" onclick="Item(this, 'mmAll')"></td></tr><tr><td><input type="checkbox" value="3" name="mm[]" class="check" id="ids3" onclick="Item(this, 'mmAll')"></td></tr><tr><td><input type="checkbox" value="4" name="mm[]" class="check" id="ids4" onclick="Item(this, 'mmAll')"></td></tr><tr><td><input type="checkbox" value="5" name="mm[]" class="check" id="ids5" onclick="Item(this, 'mmAll')"></td></tr><tr><td><input type="checkbox" value="6" name="mm[]" class="check" id="ids6" onclick="Item(this, 'mmAll')"></td></tr>
</table>
</body>
</html>
<script type="text/javascript">//Check allfunction All(e, itemName){var aa = document.getElementsByName(itemName);for (var i=0; i < aa.length; i++)aa[i].checked = e.checked; //得到那个总控的复选框的选中状态 }//Single selectfunction Item(e, allName){var all = document.getElementsByName(allName)[0];if(!e.checked) all.checked = false;else{var aa =document.getElementsByName(e.name);for (var i=0; i<aa.length; i++)if(!aa[i].checked) return;all.checked= true;}}
</script>
这篇关于js实现全选和全不选的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!