jquery Gets the checkbox property checked to undefined

Source: Internet
Author: User

Note: This article from Sina Blog, because cannot collect, therefore direct copy come over the remark, after good query

Original website:http://blog.sina.com.cn/s/blog_6810dfc20101jddq.html

When using jquery v1.10.2 to get the status of a checkbox, the output is always undefined when using. attr ("checked"). Depressed, this is a bug?!

Review the documentation for the jquery API and discover:

As of JQuery 1.6, the .attr() method returns for attributes that has not undefined been set. In addition, .attr() should is used on plain objects, arrays, the window, or the document. To retrieve and change DOM properties, use the. Prop () method.

The difference between attributes and properties can be important in specific situations. before JQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, Which could cause inconsistent behavior. As of JQuery 1.6, the .prop() method provides a-explicitly retrieve property values, while .attr() retrieves a Ttributes.

Also said: v1.6 after attr (' checked ') return to checked and undefined,v1.6 before returning true and false,v1.6 can use is (': checked ') or. Prop (' checked ') To return True and False

Summarize:

(1) method of obtaining checked

. attr (' checked '):
. Prop (' checked ')://1.6+:true/false
. Is (': Checked '):

(2) Checked assignment

All jquery versions can be assigned this way:
. attr ("Checked", "checked");
. attr ("Checked", true);

jquery1.6 or more versions of:
. Prop ("checked", true);

. Prop ("Checked", "checked");
. Prop ({checked:true});
. Prop ("Checked", function () {
return true;
});

(3) note:jquery1.6 above only exists prop ();

CheckBox Operation:

<inputID= "Cbxall"type= "checkbox"title= "Select All"name= "Cbxall"onclick= "Checkall ()" /><inputID= "Cbx1"type= "checkbox"title= "Radio"name= "Cbxtype"onclick= "Checkone ()"/>
function Checkall () {    var checkall = $ ("#cbxAll"). Prop ("checked");     if (Checkall) {
Select All $ (true); Else {
Deselect all $ (false);} }
function Checkone () {$ ("#cbxAll"). Prop ("Checked",false);}
var checkedboxes = $ ("input:checkbox[name= ' Cbxtype ']:checked");

jquery Gets the checkbox property checked to undefined

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.