


jQuery implements multi-region checkbox linkage control: Select all and reverse selection function guide
Jul 23, 2025 pm 06:39 PMCore concepts and HTML structure optimization
In complex forms or data display scenarios, we often need to perform "select all" or "select all" operations on a set of check boxes. When there are multiple independent checkbox groups on the page, each group needs to have its own "select all" function and does not affect each other. The core of implementing this function is:
- Definite area boundaries : define a common parent container for each independent checkbox group and give it a unique identity or class name so that jQuery accurately defines the operating range.
- Identify the Select All check box : Assign a specific class name to the Select All check box within each group so that it is easy to identify by the selector.
Based on the above principles, we optimize the original HTML structure, introduce the myDiv class to identify independent check box groups, and introduce the selectAll class to identify the "Select All" check box.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="actions" id="actions" title="Actions"> <!-- First checkbox group--> <div class="myDiv"> <div> <input type="checkbox" name="action" id="" class="selectAll" value="0"> Select All</div> <br> <div> <input type="checkbox" name="action" id="" class="" value="1"> Item 1</div> <br> <div> <input type="checkbox" name="action" id="" class="" value="2"> Item 2</div> <br> <div> <input type="checkbox" name="action" id="" class="" value="3"> Item 3</div> <br> </div> <!-- Second checkbox group--> <div class="myDiv"> <div> <input type="checkbox" name="action" id="" class="selectAll" value="26">Select All</div> <br> <div> <input type="checkbox" name="action" id="" class="" value="27"> Item 27</div> <br> <div> <input type="checkbox" name="action" id="" class="" value="28"> Item 28</div> <br> <div> <input type="checkbox" name="action" id="" class="" value="29"> Item 29</div> <br> <div> <input type="checkbox" name="action" id="" class="" value="30"> Item 30</div> <br> <div> <input type="checkbox" name="action" id="" class="" value="31"> Item 31</div> </div> </div>
In the above code, we wrap each independent checkbox group with myDiv class and add the selectAll class on the "Select All" checkbox for each group.
jQuery implementation logic
Next, we will use jQuery to write two main event handlers, respectively, handling click events for the "Select All" checkbox and the normal checkbox.
1. Control of the “Select All/No Select All” check box
When the user clicks the "Select All" check box, we need to synchronize the selected status of all other check boxes in the same group according to their current selected status.
$('.selectAll').on('click', function() { // Get the selected status of the current "Select All" check box let isSelected = $(this).is(':checked'); // Find the nearest parent .myDiv element upwards, and then find all checkboxes inside it $(this).parents('.myDiv').find('input[type="checkbox"]').each(function() { // Set the selected status of all check boxes in the same group consistent with the "Select All" check box $(this).prop('checked', isSelected); }); });
explain:
- $('.selectAll').on('click', function(){...}): Listen to all click events with checkboxes of the selectAll class.
- $(this).is(':checked'): Gets the selected status (true or false) of the currently clicked "Select All" check box.
- $(this).parents('.myDiv'): traverse the DOM tree up from the currently clicked "Select All" check box to find the nearest parent myDiv element. This ensures that the operation is limited to only the group where the current check box resides.
- .find('input[type="checkbox"]'): Inside the found myDiv, find all elements of input type checkbox, including the "Select All" checkbox itself.
- .each(function(){...}): Iterates through all found check boxes.
- $(this).prop('checked', isSelected): Sets the checked property of each checkbox so that it is consistent with the isSelected state.
2. Linkage control of ordinary check boxes
When the user clicks on any ordinary check box in the group, we need to dynamically update the status of the "Select All" check box based on the selected status of all ordinary check boxes in the group. The specific logic is: if all normal check boxes are selected, the "Select All" check box should also be selected; otherwise, if the "Select All" check box was originally selected, it should be unchecked.
$('input:not(".selectAll")').on('click', function() { // Get the parent.myDiv element where the current check box is located. $parentDiv = $(this).parents('.myDiv'); // Get the "Select All" check box in this group let $selectAllCheckbox = $parentDiv.find('.selectAll'); // Statistics the number of all check boxes in this group except the "Select All" check box let totalCheckboxes = $parentDiv.find('input:not(".selectAll")').length; // Statistics the number of check boxes selected in this group except for the "Select All" checkbox let checkedCheckboxes = $parentDiv.find('input:checked:not(".selectAll")').length; // If all normal check boxes are selected, select the "Select All" check box if (totalCheckboxes === checkedCheckboxes) { $selectAllCheckbox.prop('checked', true); } else { // Otherwise, uncheck the "Select All" check box $selectAllCheckbox.prop('checked', false); } });
explain:
- $('input:not(".selectAll")').on('click', function(){...}): Listen to all click events that do not have the selectAll class checkbox.
- let $parentDiv = $(this).parents('.myDiv');: Get the myDiv parent element where the normal checkbox you are currently clicking is located.
- let $selectAllCheckbox = $parentDiv.find('.selectAll');: Find the corresponding "Select All" check box inside this myDiv.
- $parentDiv.find('input:not(".selectAll")').length: counts the total number of all non-Select All check boxes in the current group.
- $parentDiv.find('input:checked:not(".selectAll")').length: Counts the number of non-Select All check boxes in the current group.
- By comparing totalCheckboxes and checkedCheckboxes, determine whether all normal check boxes are selected and update the selected status of $selectAllCheckbox accordingly.
Complete jQuery code example
Integrate the two logics above together and make sure to execute after the DOM loads:
$(document).ready(function() { // 1. Control logic of the "Select All/No Select All" check box $('.selectAll').on('click', function() { let isSelected = $(this).is(':checked'); $(this).parents('.myDiv').find('input[type="checkbox"]').prop('checked', isSelected); }); // 2. The linkage control logic of the normal check box $('input:not(".selectAll")').on('click', function() { let $parentDiv = $(this).parents('.myDiv'); let $selectAllCheckbox = $parentDiv.find('.selectAll'); let totalCheckboxes = $parentDiv.find('input:not(".selectAll")').length; let checkedCheckboxes = $parentDiv.find('input:checked:not(".selectAll")').length; // If all normal check boxes are selected, select the "Select All" check box if (totalCheckboxes === checkedCheckboxes) { $selectAllCheckbox.prop('checked', true); } else { // Otherwise, uncheck the "Select All" check box $selectAllCheckbox.prop('checked', false); } }); });
Notes and best practices
- Selector accuracy : Using a combination of .parents('.myDiv') and .find() is the key to ensuring that the operation is limited to the current group. Avoid using global selectors to prevent logical confusion between different groups.
- Event Delegation : For dynamically loaded check boxes, direct binding of click events may be invalid. In this case, it is recommended to use event delegates to bind events to parent elements:
$(document).on('click', '.selectAll', function() { /* ... */ }); $(document).on('click', 'input:not(".selectAll")', function() { /* ... */ });
This will make the event listener work properly even after the element is dynamically added to the DOM.
- Initial state handling : If some check boxes are already selected when the page is loading, you may need to perform a check in $(document).ready() to make sure the initial state of the "Select All" check box is correct. For example, you can manually trigger a normal checkbox click event (although this may not be the most elegant way) or write an initialization function to check and set.
- User Experience : Consider the behavior of the "Select All" checkbox when all sub-check boxes are disabled. Normally, if all children are not selected, the Select All check box should also be disabled or hidden.
- Performance Considerations : For complex pages with a large number of check boxes, frequent DOM operations may affect performance. However, for most common scenarios, the above approach is already efficient enough. If you encounter performance bottlenecks, you can consider using a virtual DOM library or a finer DOM operation optimization.
Summarize
Through this tutorial, we learned how to use jQuery to achieve intelligent "select all" and "anti-select" linkage functions for multi-region check boxes. The core lies in a clear HTML structure design (using a common parent container class and a specific class to identify the "Select All" checkbox), as well as precise jQuery selector and event handling logic. This not only improves the interactivity of the user interface, but also greatly simplifies the check box management tasks in front-end development. Mastering these skills will help build more robust and user-experience web applications.
The above is the detailed content of jQuery implements multi-region checkbox linkage control: Select all and reverse selection function guide. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics









You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

The steps to create an H5 click icon include: preparing a square source image in the image editing software. Add interactivity in the H5 editor and set the click event. Create a hotspot that covers the entire icon. Set the action of click events, such as jumping to the page or triggering animation. Export H5 documents as HTML, CSS, and JavaScript files. Deploy the exported files to a website or other platform.

Yes, H5 page production is an important implementation method for front-end development, involving core technologies such as HTML, CSS and JavaScript. Developers build dynamic and powerful H5 pages by cleverly combining these technologies, such as using the <canvas> tag to draw graphics or using JavaScript to control interaction behavior.

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

PS card is "Loading"? Solutions include: checking the computer configuration (memory, hard disk, processor), cleaning hard disk fragmentation, updating the graphics card driver, adjusting PS settings, reinstalling PS, and developing good programming habits.

The default style of the Bootstrap list can be removed with CSS override. Use more specific CSS rules and selectors, follow the "proximity principle" and "weight principle", overriding the Bootstrap default style. To avoid style conflicts, more targeted selectors can be used. If the override is unsuccessful, adjust the weight of the custom CSS. At the same time, pay attention to performance optimization, avoid overuse of !important, and write concise and efficient CSS code.

The methods to correctly handle this pointing in JavaScript closures include: 1. Use arrow functions, 2. Use bind methods, 3. Use variables to save this. These methods ensure that this intrinsic function correctly points to the context of the external function.
