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.