Resolving the attr (checked) of the checkbox has been a undefined problem _jquery

Source: Internet
Author: User
Recently, the cock silk should be developed for the project needs to make a fully selected CheckBox function.

Oh, this is not very simple thing, a total of checkbox,n multiple child checkbox, the Total checkbox once selected, the Child checkbox is all selected, the total once not selected, the child is not selected.

Get this small demand, this cock silk a burst of joy, how simple function Ah, OK, two minutes to finish ~ ~ ~

Time a minute of the past, this cock silk inner galloping Prairie horse, from has been gradually increased to the thousands ~ ~ ~

What the hell's going on here?
Alert ($ ("#checkbox_all"). attr ("checked");
All the time for undefined?


Nani???

Why is this?? Browser Are you stupid? Then decisively change the browser test, from Chrome to IE, from IE to Firefox. It all came out like this-_-| |

is jquery making improvements again????

Through the use of the Hubble telescope and high-definition Laser electron microscope view, finally found the clue ....

Originally, in the jquery1.6 version of this made changes:

"The Checked property is initialized when the page is initialized and does not change as the state changes."

That is, if the checkbox is selected when the page is loaded, the return is always checked (I was not selected at the beginning)

If the first is not selected, then the return is always undefined! "


Now that jquery has modified it, there's definitely a better solution:

Prop () is the tool to solve this problem!

The specific usage is as follows:

Alert ($ ("#checkbox_all"). Prop ("checked");
It will be true or false at this time ~ ~


So, the code of the cock is changed as follows:

#check_all is the total selected checkbox, #check_children为子checkbox
Copy Code code as follows:

$ ("#check_all"). Change (function () {
$ ('. Check_children '). Prop ("Checked", this.checked);
});

Or:
Copy Code code as follows:

$ ("#check_all"). Change (function () {
var is_checked = $ (this). Prop ("checked");
$ ('. Check_children '). Prop ("Checked", is_checked);
});

However, I still like to use the first method of the, the less the better the code ~~write less, do more!

It is convenient to solve the problem of the whole election ~ ~ ~ ~

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.