views:

80

answers:

3

I have an html form and i would like ALWAYS to have checkboxes to submit a value. How can i do that? I have one idea but i havent tried it and i am unsure if its the best way to do it (jquery to check if the box is checked or not, then set the value to 0/1 and check it off so it will submit)

+3  A: 

To tell you the truth, this feels like a big no-no.

Anyway here goes:

<script type="text/javascript">
$(document).ready(function () {
    $('form').submit(function() {
        $(this).find('input[type=checkbox]').each(function () {
            $(this).attr('value', $(this).is(':checked') ? '1' : '0');
            $(this).attr('checked', true);
        });
    });
});
</script>
vassilis
Good solution. I'm just wondering, why is this a big no-no?
Kranu
As @BalusC noted this goes vertically against the specification for the behavior of the checkbox. This kinda of ninja-tweaking is how major screw-ups are born.
vassilis
It turns out i cant use it. When submitting you can see the checkboxes being checked off and that might freak some people out.
acidzombie24
+2  A: 

This goes totally against the natural/specified behaviour of checkboxes in HTML. If checked, then its value will be sent as parameter. If unchecked, then its value will not be sent as parameter. Since you already know beforehand which checkboxes are all in the HTML page, you can just apply basic math to obtain the unchecked checkboxes:

uncheckedCheckboxes = allCheckboxes - checkedCheckboxes

That's also the normal practice. If you tell a bit more about the server side language you're using to process the form, we would be able to give more tips/tricks how to achieve this the best way.


Update: if those checkboxes are created dynamically at the client side, then add for each checkbox a <input type="hidden"> field containing information about the checkbox, so that the server side knows which checkboxes are all present.

BalusC
"Since you already know beforehand which checkboxes are all in the HTML page" <--- not true in my case.
acidzombie24
hmm, i would then get two values if checkbox is selected wouldnt i? The solution above already works.
acidzombie24
No, just information about that "a checkbox" is present. The state of the view should be known in the server side. Truly, the solution of vassalis works, but it's actually not a solution, it's a workaround/hack. If your code is ever going to be decoupled, reused and/or maintained by others, they will fire you because of unintuitive behaviours.
BalusC
vassilis worked but you can see the checkbox being checked off so that may freak ppl out. So i created your solution which actually is a great idea. But i only create the input if the checkbox is off. Problem is i need to remove it when i hit back otherwise i get multiple values. So i am figuring that out now.
acidzombie24
It's not necessary to add/remove them like that. You could also just keep them as "all checkboxes" and use the values of the checked checkboxes as "checked checkboxes". During submit processing, you can extract the "unchecked checkboxes" from it.
BalusC
What i do is inject a hidden input when there is an unchecked checkbox and set the name to the same name and use the value "" (or alternatively 0). Then to combat hitting back, earlier in the submit i use this solution to remove it. It works very well. http://stackoverflow.com/questions/3748340/multiple-attr-selector-in-jquery
acidzombie24
+1  A: 

There is a legitimate reason for asking for something like this, although the behaviour envisioned here is not the right way to go about it. There is a problem with the checkbox when used correctly when editing existing data and that's that there is no way to determine whether no value was submitted because the field was not present on the form or because the user cleared all of the values. You can run into this sort of problem any time you include fields conditionally.

One could go to the trouble of maintaining a "view state", of course, but it's much easier to include a hidden "companion field" whenever a checkbox or select with the multiple option (which is also excluded when all selections are cleared) is displayed. The field should have a related but different name (a name from which the actual field name can be extracted). The Lotus Domino server has used fields named %%Surrogate_FieldNameHere for this purpose since (I believe) version 7 for exactly the reason I described here.

Stan Rogers
GREAT! point. Thats exactly why i need it, if its present or not.
acidzombie24