Home > Web Front-end > JS Tutorial > How to select all in javascript

How to select all in javascript

藏色散人
Release: 2022-01-18 11:26:17
Original
6574 people have browsed it

Javascript method to implement all-select: 1. Create a form through HTML and CSS; 2. Use DOM methods to obtain the "input" elements for all-select and single-select respectively; 3. Set a variable isAllChecked; 4. Assign the value of isAllChecked to the Select All button.

How to select all in javascript

The operating environment of this article: Windows 7 system, javascript version 1.8.5, Dell G3 computer.

javascript How to select all?

Use JS to implement full selection and inverse selection in the form

This is also an exercise, which is to select all in the general e-commerce shopping cart, and add an inverse selection. (It seems that there are not many shopping carts with the inverse selection function, but Windows Explorer has the inverse selection function)

How to select all in javascript

Let’s start with selecting all:

  • When all 4 rows of products are checked, "Select All" is automatically checked; when any product is not checked, "Select All" is automatically canceled.

  • When all 4 rows of products are not checked, check "Select All" to make all 4 rows of products checked;

  • When "Select All" is checked, uncheck "Select All" and all products will be unchecked.

After sorting out the requirements, we first use HTML and CSS to draw this form. The code is as follows:

<html>
<head>
    <meta charset="utf-8">
    <title>全选2</title>
    <style type="text/css">     
        .wrap{
            margin-left: 500px;
            margin-top: 200px;
        }
        table{
            border-collapse:collapse;
        }

        th{
            border:1px solid black;
            height: 45px;   
        }

        td{
            border:1px solid black;
            text-align: center;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="wrap">  
        <table cellspacing="0" cellpadding="14">
            <thead>
                <tr>
                    <th>
                        <input type="checkbox" id="selectAll" onclick="funcAll()">
                    </th>
                    <th>商品</th>
                    <th>价格</th>
                </tr>
            </thead>
            <tbody id="j_tb">
                <tr>
                    <td>
                        <input type="checkbox" class="selectOne" onclick="funcOne()">
                    </td>
                    <td>iPhone X</td>
                    <td>8000</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" class="selectOne" onclick="funcOne()">
                    </td>
                    <td>iPad pro</td>
                    <td>5000</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" class="selectOne" onclick="funcOne()">
                    </td>
                    <td>iPad air</td>
                    <td>2000</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" class="selectOne" onclick="funcOne()">
                    </td>
                    <td>Apple watch</td>
                    <td>2000</td>
                </tr>

            </tbody>
        </table>
        <input type="button" id="backwardOption" value="反选" onclick="funcBackward()">
    </div>
</body>
</html>
Copy after login

Here I have put the function of the mouse click event After naming it, call it "funcAll()" for all selection, "funcOne()" for single selection, and "funcBackward()" for reverse selection. The CSS part mainly adds borders to the form, and at the same time blends the borders at the junction, which is basically the simplest look.

Let’s talk about requirements 2 and 3 first. The logic is relatively simple:

Use the DOM method to obtain the "input" elements for full selection and single selection respectively. If there is only one for all selection, use it directly. Just getElementById() is enough (the id has been set in advance), and there are four radio selections. Use getElementByClassName() to get it (the class has been set in advance). Of course, what you get is an array.

Assign the obtained elements to variables, then use a for loop to traverse the array, and assign the checked attribute of the variable that selects all to the checked attribute of each single-selected variable.

At this point, 2 and 3 have been solved at the same time. The code is as follows:

function funcAll(){
            var selectAll = document.getElementById(&#39;selectAll&#39;);
            var selectOnes = document.getElementsByClassName(&#39;selectOne&#39;);
            for (var i = 0; i < selectOnes.length; i++) {                 //循环遍历,把全选框的值赋给每个单选框
                selectOnes[i].checked = selectAll.checked;             
            }
        }
Copy after login

Next, let’s talk about item 1: “When all 4 rows of products are checked, “Select All” automatically is checked; when any product is not checked, "Select All" is automatically canceled."

Here, the status of "Select All" is similar to monitoring all other buttons. Once there is a change, it will be automatically changes. So this logic needs to be written into the mouse click event of the radio button of each row of products.

I set up a variable isAllChecked as a bridge. The initial state defines isAllChecked as true, and loops through each radio button. Once it is detected that a radio button is not checked, set isAllChecked to false. Break out of the loop and assign the value of isAllChecked to the Select All button.

In this way, as long as one radio button is not selected, all selections will be turned into an unselected state. , the code is as follows:

function funcOne(){                                                  
            var selectAll = document.getElementById(&#39;selectAll&#39;);            //函数作用域,所以得再定义一遍
            var selectOnes = document.getElementsByClassName(&#39;selectOne&#39;);
            var isAllChecked = true;                                        //定义一个变量作为桥梁来控制全选按钮
            for (var i = 0; i < selectOnes.length; i++) {
                if (selectOnes[i].checked === false) {
                    isAllChecked = false;
                    break;
                }
            }
            selectAll.checked = isAllChecked;
        }
Copy after login

Let’s talk about inverse selection

  1. Click the inverse selection button, and the status of all radio button buttons will be inverted: the selected ones become unselected, and the unselected ones Change the selection to
  2. and click Inverse Selection. The result will still make the selection consistent with the previous logic.

Item 1 is actually very easy to implement. When you click the invert button, cycle through the 4 radio buttons and invert the checked attribute corresponding to each element.

However, the select-all listener for all single items we implemented earlier is actually implemented in the click event of each radio button. That is to say, if we do not change the single item by clicking the radio button, The state of the selection button, Select All, is actually not monitored globally.

So, we can write the monitoring code into the reverse selection mouse click function. The final code is as follows:

            var selectAll = document.getElementById(&#39;selectAll&#39;);            //函数作用域,所以得再定义一遍
            var selectOnes = document.getElementsByClassName(&#39;selectOne&#39;);
            for (var i = 0; i < selectOnes.length; i++) {
                selectOnes[i].isAllChecked = !selectOnes[i].checked;
            }
            function funcOne(){};
            var isAllChecked = true;                                        //定义一个变量作为桥梁来控制全选按钮
            for (var i = 0; i < selectOnes.length; i++) {
                if (selectOnes[i].checked === false) {
                    isAllChecked = false;
                    break;
                }
            }
            selectAll.checked = isAllChecked;
        }
Copy after login

The final effect is as follows:

How to select all in javascript

Recommended study: "js Basic Tutorial"

The above is the detailed content of How to select all in javascript. 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