how to POST/Submit an Input Checkbox that is disabled?
I have a checkbox that, given certain conditions, needs to be disabled. Turns out HTTP doesn't post disabled inputs.
How can I get around that? submitting the input even if it's disabled and keeping the input disabled?
UPDATE: READONLY
doesn't work on checkboxes
You could use disabled="disabled"
but at this point checkbox's value will not appear into POST
values. One of the strategy is to add an hidden field holding checkbox's value within the same form and read value back from that field
Simply change disabled
to readonly
I've solved that problem.
Since readonly="readonly"
tag is not working (I've tried different browsers), you have to use disabled="disabled"
instead. But your checkbox's value will not post then...
Here is my solution:
To get "readonly" look and POST checkbox's value in the same time, just add a hidden "input" with the same name and value as your checkbox. You have to keep it next to your checkbox or between the same <form></form>
tags:
<input type="checkbox" checked="checked" disabled="disabled" name="Tests" value="4">SOME TEXT</input>
<input type="hidden" id="Tests" name="Tests" value="4" />
Also, just to let ya'll know readonly="readonly", readonly="true", readonly="",
or just READONLY
will NOT solve this! I've spent few hours to figure it out!
This reference is also NOT relevant (may be not yet, or not anymore, I have no idea): http://www.w3schools.com/tags/att_input_readonly.asp
If you're happy using JQuery then remove the disabled attribute when submitting the form:
$("form").submit(function() {
$("input").removeAttr("disabled");
});
create a css class eg:
.disable{
pointer-events: none;
opacity: 0.5;
}
apply this class instead of disabled attribute