Possible Duplicate:
Check if checkbox is checked with jQuery
I am a little bit confused about how the input of type checkbox works.
If I set a value, say 1, on a checkbox, I see that regardless of the checkbox is checked or not, the value that I get when the form is submitted is 1.
<input type="checkbox" value="1">
So, the correct way to see if a checkbox is checked is to use the checked from jQuery? Or how it can be done correctly?
Checkbox values should not get submitted if they are not checked.
If you want to check with jQuery, I would always use:
Or
With JavaScript, you have to understand the difference between attributes and properties.
checkedis both a property and an attribute. However, the attribute checked isn’t always updated when thecheckedproperty of a checkbox changes (in some browsers it is). Think of the attribute as the value in the HTML markup parsed by the DOM. Thecheckedproperty is an actual member of the checkbox element in the DOM, which always changes based on whether the box is checked or unchecked in the UI.If you want to try a foolproof method without needed JavaScript, you can try it the “MVC” way, by rendering a hidden input and a checkbox with the same name, each with appropriate “checked”-state values:
When you submit the form, if you can’t find a
myboxvalue in the request with a value of “true” then the checkbox was not checked. More about that here.