Jquery obtains the checked of the checkbox undefined resolution method

Source: Internet
Author: User

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

If a checkbox is selected, the value alert ($ (#xxx) for the property "checked" of the checkbox for alert is printed "true" instead of "checked" attr ("checked").

If not selected, print out "undefined".


Notice the red part.

$ ("#chekbox"). Sttr ("checked")//should is print "true", not "checked"

Tested to prove a problem with the above statement

The code is as follows Copy Code

<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 result is that if a checkbox is checked, the "checked" will be printed, and if the cancellation check will print out "undefined"

The experiment proves that jquery gets checked's worth printing out "true" is wrong

Extrapolate

And found that jquery has been hooked on the checkbox, is always "checked" This is confusing, if someone knows why, you can tell me, ha ~

  code is as follows copy code

<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

Note that the red position, if the default checkbox is checked state, the test will find that the check out will only output "checked", that does not understand why jquery is so


solution, or use Document.get to get it:

The code is as follows Copy Code

<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 ' to deselect to ' false '

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.