A summary of some tips for the jquery action check box (checkbox)

Source: Internet
Author: User

1. Get a single checkbox check (three ways)


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

2. Get multiple checkbox check items


$ (' Input:checkbox '). each (function () {
if ($ (this). attr (' checked ') ==true) {
Alert (This). Val ());
}
});

3. Set the first checkbox to the selected value


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

4. Set the last checkbox as the selected value


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

5. Set any checkbox to the selected value according to the index value


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

6. Select multiple checkboxes to select both 1th and 2nd checkboxes


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

7. Set checkbox to selected value according to value


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

8. Delete value=1 checkbox


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

9. Delete the first few checkboxes


$ ("Input:checkbox"). EQ (index value). Remove (); index value =0,1,2 ....
To delete a 3rd checkbox:
$ ("Input:checkbox"). EQ (2). Remove ();

10. Traversal checkbox


$ (' Input:checkbox '). each (function (index, domele) {
Write code
});

11. Select All


$ (' Input:checkbox '). each (function () {
$ (this). attr (' checked ', true);
});

12. Cancel All Selection


$ (' Input:checkbox '). each (function () {
$ (this). attr (' checked ', false);
});

13. CheckBox tag already has checked=checked but does not show tick

The checkbox tag already has checked=checked but does not show tick, the solution is to change $ ("..."). attr ("Checked", true) to $ ("..."). Prop ("checked", true).

The attr () function and the prop () function in Jquey, attr () is the value of the attribute, and prop () is the value of the property, and before JQuery 1.6, the. attr () method returns the value of some attribute. The value of the property, which results in inconsistencies. Starting with JQuery 1.6, the. Prop () method method returns the value of the property, and the. attr () method returns the value of attributes, which in the final analysis is the attribute and property.
Many attribute nodes have a corresponding property attribute, so attribute and properties can easily be confused, such as the ID and class in a DIV element are both attribute and Either way can be accessed and modified, but for the custom attribute node, or custom property, the two are not related (but in ie6-7, the two are the same, good wonderful, I hope we can all soon abandon IE8 below), need to note that For the checked attribute (attribute) does not correspond to its Checked property, attribute actually corresponds to the defaultchecked attribute and is used only to set the initial value of the check box. The attribute of the checked will not change due to the state of the check box, and checked's property will change due to the state of the check box, so in the. attr () function, even if it is set to. attr ("Checked", true), It is also used to store default values for default or checked properties, but does not change the check box to be selected and selected, which is why the checkbox label already has checked=checked but does not display the tick.

. attr (' checked '); see version 1.6+ return: "Checked" or "undefined"; 1.5-return: TRUE or False
. Prop (' checked '); 16+:true/false
. Is (': checked '); All versions: true/false//don't forget the colon.

Summary of some tips for the

jquery action check box (checkbox)

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.