Home > Web Front-end > JS Tutorial > body text

Jquery gets the checked checkbox problem_jquery

WBOY
Release: 2016-05-16 17:59:14
Original
1116 people have browsed it

Facts have proved everything. I tested it N times and found that what I said on the Internet and my previous understanding were all wrong. I wonder if you have noticed it.
Let’s take a look at what most of the information on the Internet says

Copy code The code is as follows:

Transfer something from others:
jquery determines whether the checkbox is selected
in In the checkbox of html, if selected, there will be attribute checked="checked".
If a checkbox is selected, alert($(#xxx).attr("checked")), the value of alert($(#xxx).attr("checked")), of the checkbox attribute "checked", will print out "true" instead of "checked"!
If it is not selected, "undefined" will be printed.

Pay attention to the red part, here it says
Copy the code The code is as follows:

$("#chekbox").sttr("checked")//should be print "true" , ​​not "checked"

After testing, it is proved that there is something wrong with the above statement
Copy code The code is as follows:




//The above result is: if the checkbox is checked, "checked" will be printed, and if the checkbox is unchecked, "undefined" will be printed
Experiments prove that it is wrong for Jquery to get checked value and print "true"
Apply inferences:
And it is found that Jquery gets checked checkbox and it will always be "checked". This is puzzling. If anyone If you know why, you can tell me, ha~~
Copy code The code is as follows:

< ;script type="text/javascript">
$(function() {
$("#button").click(function() {
alert($("#checkbox"). attr("checked"));
});
});



//Pay attention to the red position. If the default checkbox is in the checked state, the test will find that the checkbox is checked. Removing it will only output "checked", indicating that you don't understand why Jquery behaves like this
Solution, use document.get to get it:
Copy code The code is as follows:

<script> <br>function getcheckbox(){ <br>var test = document.getElementById("checkbox").checked; <br> alert(test); <br>} <br></script>


//Selected is "true", unchecked is "false"
If there is any mistake, please correct me . Questions searched on the Internet are all posted N times. Test it yourself to prove it.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!