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 '