Use jquery to implement inverse selection, full selection, and full deselection of checkbox

Source: Internet
Author: User

When we use js to implement web operations, full selection, no selection, and invert selection of multiple selection boxes are the most common functions, this batch operation is especially required when there is a large amount of data to bring convenient and quick services to users.

In the following example, jquery is used to implement the function of Selecting All, not selecting all, And deselection for the checkbox multiple selection box:

Suppose there are four multiple checkbox boxes, they are included in a div with ID = "Chk, the IDS of all selected, unselected, and unselected buttons are btnallchk, btnallnotchk, and btninvert.

The Code is as follows:

$ (Function () {// do not select all $ ("# btnallnotchk "). click (function () {$ ("# chk input: checkbox "). removeattr ("checked") ;}); // select $ ("# btnallchk "). click (function () {$ ("# chk input: checkbox "). ATTR ("checked", "checked") ;}); // reselect $ ("# btninvert "). click (function () {// 1. method 1: Implement invert selection $ ("# chk input: checkbox "). each (function () {This. checked =! This. checked;}) // 2. method 2: Implement invert selection // $ ("# chk input: checkbox "). each (function () {// if ($ (this ). ATTR ("checked") {// $ (this ). ATTR ("checked", false); // else {// $ (this ). ATTR ("checked", "checked"); //}) // 3. method 3: Implement invert selection // var $ CKS =$ ("# chk input: checkbox"); // For (VAR I = 0; I <$ cks. length; I ++) {// $ cks. get (I ). checked =! $ Cks. Get (I). Checked ;//}});})

 

In this way, we can select, reselect, and unselect all checkpoints.

Note: 1. jquery itself is formed by the accumulation of JavaScript code;

2. Import the jquery control before using the jquery statement.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.