JQuery checkbox (check box) Action method Rollup

Source: Internet
Author: User

1. Get a single checkbox selection (three ways to type):

The code is as follows:

$ ("input:checkbox:checked"). Val ()

Or

The code is as follows:

$ ("input:[type= ' checkbox ']:checked"). Val ();

Or

The code is as follows:

$ ("input:[name= ' ck ']:checked"). Val ();

2. Get multiple checkbox checked:

The code is as follows:

$ (' Input:checkbox '). each (function () {

if ($ (this). attr (' checked ') ==true) {

Alert ($ (this). Val ());

}

});

Or

The code is as follows:

(' Input:checkbox '). Map (function () {

Return (This). Val ();

}). Get (). Join (', ');

3. Set the first checkbox to the selected value:

The code is as follows:

$ (' Input:checkbox:first '). attr ("Checked", ' checked ');

Or

The code is as follows:

$ (' Input:checkbox '). EQ (0). attr ("Checked", ' true ');

4. Set the last checkbox to the selected value:

The code is as follows:

$ (' Input:radio:last '). attr (' checked ', ' checked ');

Or

The code is as follows:

$ (' Input:radio:last '). attr (' checked ', ' true ');

5. Set any checkbox to the selected value based on the index value:

The code is as follows:

$ (' Input:checkbox). EQ (index value). attr (' checked ', ' true '); index value =0,1,2 ....

Or

The code is as follows:

$ (' Input:radio '). Slice (1,2). attr (' checked ', ' true ');

6. Select multiple checkbox:

Select both the 1th and 2nd checkbox:

The code is as follows:

$ (' Input:radio '). Slice (0,2). attr (' checked ', ' true ');

7. Set checkbox to selected value based on value:

The code is as follows:

$ ("input:checkbox[value= ' 1 ']"). attr (' checked ', ' true ');

8. Delete the checkbox for value=1:

The code is as follows:

$ ("input:checkbox[value= ' 1 ']"). Remove ();

9. Delete the first few checkbox:

The code is as follows:

$ ("Input:checkbox"). EQ (index value). Remove (); index value =0,1,2 ....

If you delete a 3rd checkbox:

The code is as follows:

$ ("Input:checkbox"). EQ (2). Remove ();

10. Traverse checkbox:

The code is as follows:

$ (' Input:checkbox '). each (function (index, domele) {

Write code

});

11. All Selected

The code is as follows:

$ (' Input:checkbox '). each (function () {

$ (this). attr (' checked ', true);

});

12. Cancel all options:

The code is as follows:

$ (' Input:checkbox '). each (function () {

$ (this). attr (' checked ', false);

});

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.