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.