Home >Web Front-end >JS Tutorial >How to select all in javascript
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.
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)
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>
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('selectAll'); var selectOnes = document.getElementsByClassName('selectOne'); for (var i = 0; i <p> 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." </p><p>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. </p><p>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. </p><p>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: </p><pre class="brush:php;toolbar:false">function funcOne(){ var selectAll = document.getElementById('selectAll'); //函数作用域,所以得再定义一遍 var selectOnes = document.getElementsByClassName('selectOne'); var isAllChecked = true; //定义一个变量作为桥梁来控制全选按钮 for (var i = 0; i <h3>Let’s talk about inverse selection</h3><blockquote><ol> <li>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 </li> <li> and click Inverse Selection. The result will still make the selection consistent with the previous logic. </li> </ol></blockquote><p>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. </p><p>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. </p><p>So, we can write the monitoring code into the reverse selection mouse click function. The final code is as follows: </p><pre class="brush:php;toolbar:false"> var selectAll = document.getElementById('selectAll'); //函数作用域,所以得再定义一遍 var selectOnes = document.getElementsByClassName('selectOne'); for (var i = 0; i <p>The final effect is as follows: </p><p> <img src="https://img.php.cn/upload/image/773/281/563/1642476251809927.png" title="1642476251809927.png" alt="How to select all in javascript"></p><p> Recommended study: "<a href="https://www.php.cn/course/list/2.html" target="_blank">js Basic Tutorial</a>"</p>
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!