在網上看了別人寫的一些實現全選和反選的函數總覺的寫的不太實用而且寫的複雜,給一些初學者看都會頭暈,所以我今天寫下個比較實用的函數,如下:
- <script>
- //全選與反選
- function selectOrClearAll() {
- var userListTable = document.getElementById("userListTable");
- var input = userListTable.getElementsByTagName("input");
- for (var i = 0; i < input.length; i++) {
- if (input[i].type.toLowerCase() == "checkbox") {
- if (input[i].checked == true) {
- input[i].checked = false;
- } else {
- input[i].checked = true;
- }
- }
- }
- }
- </script>
全選與反選只需調用這一個函數就可搞定