JavaScript to achieve full or reverse selection effect in form

JavaScript to achieve full or reverse selection effect in form

This article shares the specific code of JavaScript to achieve full or reverse selection of the form for your reference. The specific content is as follows

Checkboxes are often used in forms. It is also very common to select all or unselect all lists made by checkboxes, such as the product list in a shopping cart. Therefore, this article also introduces, demonstrates and analyzes the selection effect of this common function and its underlying implementation ideas and codes. The following is the JS code:

<script>
        //Example of selecting all or unselecting all in a form: Note: If the checked attribute value of a checkbox is true, it is selected, and if it is false, it is unselected var j_cbAll = document.getElementById('j_cbAll'); //Get the select all button var j_tbs = document.getElementById('j_tb').getElementsByTagName('input'); //Get all input checkboxes //Register events //1. Event of the select all button j_cbAll.onclick = function () {
            // this.checked can get the status of the current checkbox and return a value of true or false
            for (var i = 0; i < j_tbs.length; i++) {
                j_tbs[i].checked = this.checked; // Make the checked value of each checkbox in tbody equal to the checked value of the current select all button, so their states will be consistent}
        }
        // 2. Events of the checkboxes in tbody: If all the checkboxes below are selected, the Select All button will also be selected. If at least one is not selected, the Select All button will also be unselected. Note: Every time a checkbox in tbody is clicked, a check needs to be made to see if all the checkboxes are selected for (var i = 0; i < j_tbs.length; i++) {
            j_tbs[i].onclick = function () {
                // flag is used to control whether the select all button is selected var flag = true;
                // Each time you click the checkbox below, you need to loop and check if all 4 checkboxes are selected for (var i = 0; i < j_tbs.length; i++) {
                    console.log('---'+j_tbs[i].checked);
                    if (j_tbs[i].checked == false) { // If a button is not selected, execute the internal code to modify the flag value to false
                        flag = false;
                        break; // Exit the inner for loop, which can improve execution efficiency, because as long as one is not selected, the rest do not need to be judged.}
                }
                j_cbAll.checked = flag; //After the loop is finished, set the flag value to the select all button}
        }
</script>

Execution code screenshot:

When you click Select All: Click again:

When the checkbox below is checked:

Note: If all four check boxes below are not selected, the Select All button above will not be selected.

The internal JS implementation idea is mainly divided into two parts: the first part is the function of the "Select All" button. When it is selected, all the check boxes below are checked = true. Note: this value is the key to the implementation of this function. If the "Select All" button is unchecked, its checked value is false, so this value can be assigned to all the sub-check boxes below; the second part is the state of the "Select All" button determined by the click selection function of the sub-check boxes below and their states. If all the following are selected, the Select All button must also be selected. If at least one is unchecked, the Select All button is also unchecked. Note: Every click of a sub-check box requires a judgment to see whether all the check boxes are selected. (It is recommended to combine code analysis, the code contains detailed comment analysis)

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:
  • Example of implementing select all, deselect and delete operations of form items using js
  • ReactJS implements single-select, multiple-select and reverse-select examples in forms

<<:  How to implement nested if method in nginx

>>:  Solve the problem of MySQL 8.0 still prompting Access denied even though the input is correct

Recommend

mysql splits a row of data into multiple rows based on commas

Table of contents Separation effect Command line ...

Summary of MySQL database like statement wildcard fuzzy query

MySQL error: Parameter index out of range (1 >...

A brief discussion on the role of Vue3 defineComponent

Table of contents defineComponent overload functi...

A brief analysis of the use of zero copy technology in Linux

This article discusses several major zero-copy te...

Mysql Sql statement comments

You can add comments to MySQL SQL statements. Her...

Detailed explanation of the difference between alt and title

These two attributes are often used, but their di...

Detailed explanation of Linx awk introductory tutorial

Awk is an application for processing text files, ...

Detailed tutorial on compiling and installing MySQL 8.0.20 from source code

In the previous article, we introduced: MySQL8.0....

Use Python to connect to MySQL database using the pymysql module

Install pymysql pip install pymysql 2|0Using pymy...

Analysis of MySQL concurrency issues and solutions

Table of contents 1. Background 2. Slow query cau...

Detailed explanation of scheduled tasks for ordinary users in Linux

Preface Ordinary users define crontab scheduled t...

MySQL backup table operation based on Java

The core is mysqldump and Runtime The operation i...

Three networking methods and principles of VMware virtual machines (summary)

1. Brigde——Bridge: VMnet0 is used by default 1. P...

Vue implements horizontal scrolling of marquee style text

This article shares the specific code for Vue to ...

5 things to note when writing React components using hooks

Table of contents 01. Use useState when render is...