Tuesday, 12 April 2016

javascript - Testing if a checkbox is checked with jQuery



If the checkbox is checked, then I only need to get the value as 1; otherwise, I need to get it as 0. How do I do this using jQuery?



$("#ans").val() will always give me one right in this case:






Answer



Use .is(':checked') to determine whether or not it's checked, and then set your value accordingly.



More information here.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...