How does Jquery obtain the checked of the checkbox?

Source: Internet
Author: User

Facts have proved everything. I tested it N times and found that the online statement and my previous understanding are all wrong.
Let's take a look at most of the online documents.
Copy codeThe Code is as follows:
Something about others:
Jquery checks whether checkbox is selected
In the checkbox of html, the selected property will be checked = "checked ".
If a checkbox is selected, alert indicates the value of alert ($ (# xxx) for the checkbox attribute "checked ). attr ("checked") will print "true" instead of "checked "!
If not selected, the output is "undefined ".

Pay attention to the red part.
Copy codeThe Code is as follows:
$ ("# Chekbox"). sttr ("checked") // shocould be print "true", not "checked"

The test proves that the above statement has a problem.
Copy codeThe Code is as follows:
<Script type = "text/javascript">
$ (Function (){
$ ("# Button"). click (function (){
Alert ($ ("# checkbox"). attr ("checked "));
});
});
</Script>
<Input type = "checkbox" name = "checkbox" id = "checkbox"> <input type = "button" id = "button" value = "Click Me">

// The above result is: If checkbox is checked, "checked" is printed. If it is canceled, "undefined" is printed"
The experiment proves that it is wrong for Jquery to obtain the checked value and print "true ".
Trigger:
It is also confusing to find the checkbox that has been checked by Jquery. If anyone knows why, let me know ~~
Copy codeThe Code is as follows:
<Script type = "text/javascript">
$ (Function (){
$ ("# Button"). click (function (){
Alert ($ ("# checkbox"). attr ("checked "));
});
});
</Script>
<Input type = "checkbox" name = "checkbox" id = "checkbox" checked> <input type = "button" id = "button" value = "Click Me">

// Pay attention to the red position. If the checkbox is in the checked status by default, the test will find that if you remove the check box, only "checked" will be output, indicating that you do not understand why Jquery is like this.
Solution: Use document. get to get the solution:
Copy codeThe Code is as follows:
<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 "() ">

// Select "true". deselect "false"
If there is a mistake, you need to correct it. I search for a single post over N times. I will test it to prove it.

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.