jquery implements selection check

PHPz
Release: 2023-05-28 14:49:37
Original
1089 people have browsed it

jQuery is a very popular JavaScript library, which can help us easily handle various operations such as HTML, CSS, events, etc., and is especially suitable for realizing dynamic effects in web applications.

In many web applications, it is often necessary to use check boxes to implement functions such as batch selection or multi-condition filtering. Therefore, this article will introduce how to use jQuery to implement the function of selecting checkboxes.

First, we need to prepare some HTML code. Suppose we have a list where each list item has a checkbox, and we want to be able to select all checkboxes by clicking the Select All button. The HTML code is as follows:

<ul>
  <li><input type="checkbox"> 选项1</li>
  <li><input type="checkbox"> 选项2</li>
  <li><input type="checkbox"> 选项3</li>
  <li><input type="checkbox"> 选项4</li>
  <li><input type="checkbox"> 选项5</li>
</ul>

<button id="select-all">全选</button>
Copy after login

In the above HTML code, we use a <ul> element to display the list items and add a checkbox to each list item . In addition, we also added a button <button>, which will select all check boxes when clicked.

Next, we can use jQuery to implement the function of selecting the check box. The specific implementation is as follows:

$(function() {
    // 获取全选按钮
    var selectAll = $('#select-all');

    // 获取所有复选框
    var checkboxes = $('input[type="checkbox"]');

    // 全选按钮点击事件
    selectAll.click(function() {
        if($(this).prop('checked')) {
            checkboxes.prop('checked', true);
        } else {
            checkboxes.prop('checked', false);
        }
    });

    // 复选框点击事件
    checkboxes.click(function() {
        if(checkboxes.length == checkboxes.filter(':checked').length) {
            selectAll.prop('checked', true);
        } else {
            selectAll.prop('checked', false);
        }
    });
});
Copy after login

In the above code, we first use jQuery's selector to get the select all button and all check boxes. Then, we added the click event of the Select All button. When the Select All button is selected, all check boxes will be selected; when the Select All button is not selected, all check boxes will be unchecked.

In the click event of the check box, we determine whether the number of currently selected check boxes is equal to the number of all check boxes. If so, it is considered that all check boxes have been selected. This The Select All button will be automatically selected; otherwise, the Select All button will not be selected.

Through the above code, we can already implement the function of selecting check boxes. However, we can further improve this feature. For example, when all check boxes are selected, the state of the Select All button can be changed to "Unselect All", and when at least one check box is selected, the state of the Select All button can be changed to "Select All" . The specific code is as follows:

$(function() {
    // 获取全选按钮
    var selectAll = $('#select-all');

    // 获取所有复选框
    var checkboxes = $('input[type="checkbox"]');

    // 全选按钮点击事件
    selectAll.click(function() {
        if($(this).prop('checked')) {
            checkboxes.prop('checked', true);
            $(this).val('取消全选');
        } else {
            checkboxes.prop('checked', false);
            $(this).val('全选');
        }
    });

    // 复选框点击事件
    checkboxes.click(function() {
        if(checkboxes.length == checkboxes.filter(':checked').length) {
            selectAll.prop('checked', true);
            selectAll.val('取消全选');
        } else {
            selectAll.prop('checked', false);
            selectAll.val('全选');
        }
    });
});
Copy after login

In the above code, we added changes to the text and state of the select all button in the select all button click event and check box click event respectively. When all checkboxes are selected, the text of the Select All button will be changed to "Unselect All", and when any checkbox is unchecked, the text of the Select All button will be changed to "Select All".

Through the above code, we can easily implement the function of selecting check boxes. In actual applications, we can flexibly adjust the style and logic of the selection check box according to specific needs and page layout to meet different application scenarios.

The above is the detailed content of jquery implements selection check. For more information, please follow other related articles on the PHP Chinese website!

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