How does jquery obtain the attr (checked) of the checkbox is always a solution to the undefined problem?

Source: Internet
Author: User

How does jquery obtain the attr (checked) of the checkbox is always a solution to the undefined problem?

Using jquery to determine whether checkbox is selected, you may think of the following method immediately:

$ ("# Phpernote"). attr ("checked ")

But today, I found that this statement always returns undefined, which is hard to understand. I checked it online and found it was like this.

This method was modified in jquery1.6 and later versions because:

The checked attribute has been initialized during page initialization and will not change as the status changes.

That is to say, if the checkbox is selected after loading the page, the returned result will always be checked (I did not select it at the beginning)

If not selected at the beginning, the returned result will always be undefined!

Therefore, jquery uses the. prop () method to solve this problem after making this change.

The specific usage of. prop () is as follows:

Alert ($ ("# phpernote"). prop ("checked "));

The returned result is true or false.

Therefore, you can write all the common options at work as follows:

Add # check_all to the total checkbox selected, and. check_children to the subcheckbox.

$("#check_all").change(function(){$('.check_children').prop("checked",this.checked);});

Or:

$("#check_all").change(function(){ var is_checked=$(this).prop("checked"); $('.check_children').prop("checked",is_checked); });
Articles you may be interested in
  • How to align check boxes (checkbox) and single-digit (radio) horizontally and vertically with the text
  • Jquery obtains the checkbox selected values (one group and one)
  • Common Values of Cache-control include private, no-cache, max-age, and must-revalidate.
  • Solution to the error "event is not defined" prompt in Firefox
  • Select into from prompt Undeclared variable... solution to the error
  • Solution to cookie session failure under IE Frame
  • Differences between null and not null in mysql and efficiency issues
  • Completely solves the adaptive width problem of the Button in IE6 and 7.

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.