Home > Web Front-end > JS Tutorial > Select all checkbox JavaScript writing (graphic tutorial)

Select all checkbox JavaScript writing (graphic tutorial)

亚连
Release: 2018-05-19 09:38:49
Original
1382 people have browsed it

This article mainly introduces the JavaScript writing of the "Select All" checkbox. The html and js codes are attached for everyone to have a deeper understanding. You can check the detailed explanation below for the specific operation steps. Interested friends can For reference.

There are two situations for the operation of the select all box:

1. Click the select all box and select all below

2. Click the check box below, click on all, the all selection box is selected, otherwise the all selection box is not selected.

html style

<tr>
     <td>爱  好</td>
     <td>
      <label for=""><input type="checkbox" name="fav" id="" value="苹果" class="btn"/>苹果</label>
     </td>
     <td>
      <label for=""><input type="checkbox" name="fav" id="" value="香蕉" class="btn"/>香蕉</label>
     </td>
     <td>
      <label for=""><input type="checkbox" name="" id="checkAll" value="全选" class="btn"/>全选</label>
     </td>
    </tr>
Copy after login

js style

var oChkAll = document.getElementById("checkAll");
    //全选
    oChkAll.onclick = function() {
     for(var i = 0; i < oFav.length; i++) {
      oFav[i].checked = this.checked;
     }
    }
    //复选框组
    for(var i = 0; i < oFav.length; i++) {
     oFav[i].onclick = function() {
      //如果全选
      if(isChkAll()) {
       oChkAll.checked = true;
      } else {
       oChkAll.checked = false;
      }
     }
    }
    //判断是否全选
    function isChkAll() {
     var all = oFav.length;
     var chk = 0;
     for(var i = 0; i < oFav.length; i++) {
      if(oFav[i].checked) {
       chk++;
      }
     }
     if(all == chk) {
      return true;
     } else {
      return false;
     }
    }
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

jsTips for using DOM event binding

##jsSummary of parsing data skills

Detailed explanation of the use of JS prototype and prototype chain

The above is the detailed content of Select all checkbox JavaScript writing (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template