I've got a load of checkboxes that are checked by default. My users will probably uncheck a few (if any) of the checkboxes and leave the rest checked.
Is there any way to make the form POST the checkboxes that are not checked, rather than the ones that are checked?
What I did was a bit different. First I changed the values of all the unchecked checkboxes. To "0", then selected them all, so the value would be submitted.
The solution I liked the most so far is to put a hidden input with the same name as the checkbox that might not be checked. I think it works so that if the checkbox isn't checked, the hidden input is still successful and sent to the server but if the checkbox is checked it will override the hidden input before it. This way you don't have to keep track of which values in the posted data were expected to come from checkboxes.
Example on Ajax actions is(':checked') used jQuery instead of .val();
params will get value in TRUE OR FALSE..
There is a better workaround. First of all provide name attribute to only those checkboxes that are checked. Then on click
submit
, through a JavaScriptfunction
you check all unchecked boxes . So when yousubmit
only those will be retrieved inform collection
those who havename
property.Advantage: No need to create extra hidden boxes,and manipulate them.
jQuery version of @vishnu's answer.
If you are using jQuery 1.5 or below please use the .attr() function instead of .prop()
You can also intercept the form.submit event and reverse check before submit