JAVASCRIPT +CHECKBOX實現全選與反選函數--CTO

 在網上看了別人寫的一些實現全選和反選的函數總覺的寫的不太實用而且寫的複雜,給一些初學者看都會頭暈,所以我今天寫下個比較實用的函數,如下:

  1. <script>
  2.    //全選與反選
  3.    function selectOrClearAll() {
  4.     var userListTable = document.getElementById("userListTable");
  5.     var input = userListTable.getElementsByTagName("input");
  6.     for (var i = 0; i < input.length; i++) {
  7.         if (input[i].type.toLowerCase() == "checkbox") {
  8.             if (input[i].checked == true) {
  9.                 input[i].checked = false;
  10.             } else {
  11.                 input[i].checked = true;
  12.             }
  13.         }
  14.     }
  15.    } 
  16. </script>

全選與反選只需調用這一個函數就可搞定

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章