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)
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- How to fix IE ClearType + jQuery opacity problem i
- jQuery add and remove delay
- Is there a way to play audio on a mobile browser w
if you have many checkbox, you can try this code:
If you have the following HTML:
Normal form submit:
On form submit, before submitting, change all values of checkboxes to 0 and 1 based on if checkbox is unchecked or checked. Like so:
Note: Ugly thing about this code, while form is submitting, all checkboxes will appear as "checked" for a moment. But if you apply same concept for ajax form submit, it would be better.
AJAX form submit:
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 itsvalue
will not be sent as parameter. How else would you in the server side be able to distinguish checked from unchecked checkboxes? Note that when thevalue
is unspecified, then most browsers default to "on". It's easier if you give all checkboxes the samename
but a different and fixedvalue
. This way you can obtain the checked ones as an array/collection.If all checkboxes are already known beforehand in server side, you can just apply basic math to obtain the unchecked checkboxes:
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.
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 as the moment of submission.Although this goes against the HTML spec, if you know what you are doing, using this you no longer have to cater checkboxes which are handled completely differently when submitted - and for example naming fields
with_brackets[]
can actually be useable.Complete solution
Take note: the non-checked checkboxes now submit a value of
"0"
Additionally, if you want to change the behaviour of a single form only, just alter the first line in the above snippet:
To tell you the truth, this feels like a big no-no.
Anyway here goes:
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.