jquery checkbox repeated calls to attr (' checked ', true/false) only take effect for the first time

Source: Internet
Author: User


  1. /**
  2. * Select All
  3. */
  4. function Checkall () {
  5. $ ("input[name=ids]"). attr ("Checked", true);
  6. }
  7. /** 
  8. * All not selected
  9. */
  10. function Uncheckall () {
  11. $ ("input[name=ids]"). attr ("Checked", false);
  12. }

Problem Description:

The initial status check box does not have a full selection,

Click the Select All button to call the Checkall method,

A full selection is achieved,

Then click the Uncheck All button,

To achieve a complete no-election,

Then click the Select All button again,

The result is a full selection of wood,

Then repeatedly click on the wood to have any reaction.

Google's:

Replace the attr method with the prop method,


    1. /**
    2. * Select All
    3. */
    4. function Checkall () {
    5. $ ("input[name=ids]"). Prop ("checked", true);
    6. }

It worked.

Prop () Gets the property value of the matching element.
This method is jquery1.6 later, used to differentiate the previous. attr () method.

One of the biggest differences is the Boolean attribute, which is used after 1.6. The prop () method is fine.
This boolean attribute, again explained, is the attribute value only True|false property.
There is also the case that only the attribute name is added, and the same can be used without writing property values. The prop () method. For example: checked, disable and so on, in fact, they are still belong to the Boolean type attribute.

1. Add property name This property will take effect should use Prop ();
2. There are true,false two attributes using prop ();
3. The other uses attr ();

jquery checkbox repeated calls to attr (' checked ', true/false) only take effect for the first time

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.