This article shares the specific code of JavaScript to select or cancel all checkboxes for your reference. The specific content is as follows Implementation ideas 1. Get the total selection box and all small selection box element objects Note: In HTML, the selected state is checked = "checked", but in JS, the selected state - - - checked = true; the unchecked state - - - checked = false; Suggestion: For this kind of attribute value, you can print it in the console to see what the value is Code Sample<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Multiple Selection Box</title> <style> .box { width: 300px; margin: 100px auto; } thead { color: #fff; background-color: #008dd0; } </style> </head> <body> <div class="box"> <table border="1" cellspacing="0" cellpadding="5" width="200" align="center"> <thead> <tr> <th><input type="checkbox" value="0" id="cbAll"></th> <th>Sports</th> </tr> </thead> <tbody id="tb"> <tr> <td><input type="checkbox" value="1"></td> <td>Running</td> </tr> <tr> <td><input type="checkbox" value="2"></td> <td>Rope skipping</td> </tr> <tr> <td><input type="checkbox" value="3"></td> Yoga </tr> <tr> <td><input type="checkbox" value="4"></td> <td>Swimming</td> </tr> <tr> <td><input type="checkbox" value="5"></td> Cycling </tr> </tbody> </table> </div> <script> var all = document.querySelector('#cbAll'); var sports = document.querySelector('#tb').querySelectorAll('input'); // Bind click event to the select all button all.onclick = function() { console.log(all.checked); for (var i = 0; i < sports.length; i++) { sports[i].checked = all.checked; } } // Bind click events to each small checkbox for (var i = 0; i < sports.length; i++) { sports[i].onclick = function() { // Control whether the select all button is selected var flag = true; // Each time a small box is clicked, check if all checkboxes are checked for (var i = 0; i < sports.length; i++) { if (!sports[i].checked) { flag = false; break; // As long as one small checkbox is not selected, the Select All button is not selected, and the loop can be jumped out. The following small checkboxes do not need to be judged again} } all.checked = flag; } } </script> </body> </html> Page effect: The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Several ways to connect tables in MySQL
>>: Detailed explanation of how to use Nginx + consul + upsync to achieve dynamic load balancing
Overflow Hide It means hiding text or image infor...
Table of contents What is MVCC MVCC Implementatio...
Table of contents 1. Global level 2. Database lev...
1. Enter the directory where your project war is ...
Using DOSBox, you can simulate DOS under Windows ...
I have been in contact with PHP for so long, but ...
A few days ago, when I was adjusting a module of a...
Because I need to use Ubuntu+Python 3.6 version t...
The MySQL built-in date function TIMESTAMPDIFF ca...
1: Install mongodb in docker Step 1: Install mong...
Before the arrow was shot, the bow whispered to t...
It is mainly the configuration jump of the if jud...
Today, this article introduces some basic concept...
Use the Linux utility certbot to generate https c...
The Spring Boot project uses docker containers, j...