Checkbox 'off' value without JavaScript
Does anyone have a no JavaScript way to make HTML checkbox still submit a value? As in if you check it the form submission will include a value A but if it is uncheck it still contains a value B?
While I figure开发者_开发技巧 there isn't any way, I'm working on a site which needs to still be function when JS is off, and this would be ideal.
HTML checkboxes aren't supposed to work that way. If you check the checkbox, the definied parameter name=value pair will be sent. If you uncheck it, it will not be sent. In the server side you just have to check the presence of the parameter name (or value in case of multiple checkboxes in a named group) in the parameter map and handle accordingly. If present, then it's checked. If absent, then it's not checked. Simple as that. You already know the "unchecked value" in the server side.
You might be able to do it by having a hidden form element with the same name
. However, different browsers may treat the priority of which one gets submitted (if the checkbox is checked) differently, so at best, it's a hacky solution.
The way I worked one of these problems was to set a hidden form element that included all of the checkbox names
delimited with a comma. Then, on the server side, I simply exploded this string and checked to see which checkboxes were on and off. It still felt like a hack, however, but I wasn't familiar with JavaScripts at the time, so that's what I ended up doing. Considering revising this at this time.
I hope this will help someone.
精彩评论