jquery gets the checked state of a checkbox

Source: Internet
Author: User

This depressed, today write this function when found the problem, the Internet to find a lot of data control, more tangled ...

It turns out that everything, I tested n times, found on the Internet and their previous understanding is wrong, do not know that we have not found.

Here's a look at most of the information on the Internet

Some things to turn to others:

jquery Determines if checkbox is selected

In the HTML checkbox, the selected word will have the attribute checked= "checked".

If a checkbox is selected, alert ($ (#xxx) to the value of the CheckBox's property "Checked" and "attr" ("Checked") will print "true" instead of "checked"!

If not selected, the print is "undefined".


Pay attention to the red part, here it says

Should is print "true", not  "checked"

Tested to prove there was a problem with the above statement

<script type= "Text/javascript" >


Alert ($ ("#checkbox"). attr ("checked"));
});
});
</script>

<input type= "checkbox" name= "checkbox" id= "checkbox" ><input type= "button" id= "button" value= "click Me" >
The result is: If the checkbox is checked, the "checked" will be printed out, if the cancel tick will print "undefined"

Experiment proves that Jquery gets checked worth printing out "true" is wrong

Extrapolate

and finding that jquery gets a checkbox that has been hooked is always "checked" (cause: The problem with the jquery version, the version after jquery.1.6, and the problem) ~ ~

<script type= "Text/javascript" >


Alert ($ ("#checkbox"). attr ("checked"));
});
});
</script>

<input type= "checkbox" name= "checkbox" id= "checkbox" Checked><input type= "button" id= "button" value= "click Me ">
Note the red position, if the default checkbox is the checked state, the test will find that removing the tick will only output "checked", indicating that it does not understand why jquery


workaround : Or use the document.get to get it:

<script>
function Getcheckbox () {
var test = document.getElementById ("checkbox"). Checked;
alert (test);
}
</script>
<input type= "checkbox" name= "checkbox" id= "checkbox" ><input type= "button" id= "button" value= "click Me" Onclick= "Getcheckbox ()" >
Check "true" to uncheck "false"



If there is any mistake, I hope to correct. Online Search questions are a post to n times, self test prove

Reprint:http://www.cnblogs.com/-run/archive/2011/11/16/2251250.html

jquery gets the checked state of a checkbox

Related Article

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.