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?
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
<input name="foo" value="bar" readonly="readonly" />
–
Enshroud readonly
won't work since you won't be able to post the value of a checkbox with such tag, use disabled
along with hidden input element to hold checkbox's value, see how: https://mcmap.net/q/121673/-how-to-post-submit-an-input-checkbox-that-is-disabled –
Venterea 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");
});
<select>
or <textfield>
or other HTML elements that support the disabled
attribute, you can select them and enable all with: $( "*:disabled" ).removeAttr("disabled");
–
Peterson $("input[type=checkbox]:disabled").prop("disabled", false);
–
Extemporize create a css class eg:
.disable{
pointer-events: none;
opacity: 0.5;
}
apply this class instead of disabled attribute
You could handle it this way... For each checkbox, create a hidden field with the same name
attribute. But set the value of that hidden field with some default value that you could test against. For example..
<input type="checkbox" name="myCheckbox" value="agree" />
<input type="hidden" name="myCheckbox" value="false" />
If the checkbox is "checked" when the form is submitted, then the value of that form parameter will be
"agree,false"
If the checkbox is not checked, then the value would be
"false"
You could use any value instead of "false", but you get the idea.
The simplest way to this is:
<input type="checkbox" class="special" onclick="event.preventDefault();" checked />
This will prevent the user from being able to deselect this checkbox and it will still submit its value when the form is submitted. Remove checked if you want the user to not be able to select this checkbox.
Also, you can then use javascript to clear the onclick once a certain condition has been met (if that's what you're after). Here's a down-and-dirty fiddle showing what I mean. It's not perfect, but you get the gist.
<input type="checkbox" checked="checked" onclick="this.checked=true" />
I started from the problem: "how to POST/Submit an Input Checkbox that is disabled?" and in my answer I skipped the comment: "If we want to disable a checkbox we surely need to keep a prefixed value (checked or unchecked) and also we want that the user be aware of it (otherwise we should use a hidden type and not a checkbox)". In my answer I supposed that we want to keep always a checkbox checked and that code works in this way. If we click on that ckeckbox it will be forever checked and its value will be POSTED/Submitted! In the same way if I write onclick="this.checked=false" without checked="checked" (note: default is unchecked) it will be forever unchecked and its value will be not POSTED/Submitted!.
Don't disable it; instead set it to readonly, though this has no effect as per not allowing the user to change the state. But you can use jQuery to enforce this (prevent the user from changing the state of the checkbox:
$('input[type="checkbox"][readonly="readonly"]').click(function(e){
e.preventDefault();
});
This assumes that all the checkboxes you don't want to be modified have the "readonly" attribute. eg.
<input type="checkbox" readonly="readonly">
This works like a charm:
The only disadvantage is the short flashing up of the disabled input fields when submitting. At least in my scenario that isn´t much of a problem!
$('form').bind('submit', function () {
var $inputs = $(this).find(':input'),
disabledInputs = [],
$curInput;
// remove attributes
for (var i = 0; i < $inputs.length; i++) {
$curInput = $($inputs[i]);
if ($curInput.attr('disabled') !== undefined) {
$curInput.removeAttr('disabled');
disabledInputs.push(true);
} else
disabledInputs.push(false);
}
// add attributes
setTimeout(function() {
for (var i = 0; i < $inputs.length; i++) {
if (disabledInputs[i] === true)
$($inputs[i]).attr('disabled', true);
}
}, 1);
});
Since:
I'm gonna offer you another possibility:
Set your checkbox as disabled as normal. And before the form is submitted by user enable this checkbox by JavaScript.
<script>
function beforeSumbit() {
var checkbox = document.getElementById('disabledCheckbox');
checkbox.disabled = false;
}
</script>
...
<form action="myAction.do" method="post" onSubmit="javascript: beforeSubmit();">
<checkbox name="checkboxName" value="checkboxValue" disabled="disabled" id="disabledCheckbox" />
<input type="submit />
</form>
Because the checkbox is enabled before the form is submitted, its value is posted in common way. Only thing that isn't very pleasant about this solution is that this checkbox becomes enabled for a while and that can be seen by users. But it's just a detail I can live with.
There is no other option other than a hidden
field, so try to use a hidden
field like demonstrated in the code below:
<input type="hidden" type="text" name="chk1[]" id="tasks" value="xyz" >
<input class="display_checkbox" type="checkbox" name="chk1[]" id="tasks" value="xyz" checked="check" disabled="disabled" >Tasks
Unfortunately there is no 'simple' solution. The attribute readonly cannot be applied to checkboxes. I read the W3 spec about the checkbox and found the culprit:
If a control doesn't have a current value when the form is submitted, user agents are not required to treat it as a successful control. (http://www.w3.org/TR/html401/interact/forms.html#h-17.13.2)
This causes the unchecked state and the disabled state to result in the same parsed value.
The only full-proof way to do this is to add a hidden input with the same name with javascript on submitting the form.
You can use the small snippet I made for this:
function disabled_checkbox() {
var theform = document.forms[0];
var theinputs = theform.getElementsByTagName('input');
var nrofinputs = theinputs.length;
for(var inputnr=0;inputnr<nrofinputs;inputnr++) {
if(theinputs[inputnr].disabled==true) {
var thevalueis = theinputs[inputnr].checked==true?"on":"";
var thehiddeninput = document.createElement("input");
thehiddeninput.setAttribute("type","hidden");
thehiddeninput.setAttribute("name",theinputs[inputnr].name);
thehiddeninput.setAttribute("value",thevalueis);
theinputs[inputnr].parentNode.appendChild(thehiddeninput);
}
}
}
This looks for the first form in the document, gathers all inputs and searches for disabled inputs. When a disabled input is found, a hidden input is created in the parentNode with the same name and the value 'on' (if it's state is 'checked') and '' (if it's state is '' - not checked).
This function should be triggered by the event 'onsubmit' in the FORM element as:
<form id='ID' action='ACTION' onsubmit='disabled_checkbox();'>
You can keep it disabled as desired, and then remove the disabled attribute before the form is submitted.
$('#myForm').submit(function() {
$('checkbox').removeAttr('disabled');
});
Add onsubmit="this['*nameofyourcheckbox*'].disabled=false"
to the form.
Adding onclick="this.checked=true"
Solve my problem:
Example:
<input type="checkbox" id="scales" name="feature[]" value="scales" checked="checked" onclick="this.checked=true" />
I just combined the HTML, JS and CSS suggestions in the answers here
HTML:
<input type="checkbox" readonly>
jQuery:
(function(){
// Raj: https://mcmap.net/q/121673/-how-to-post-submit-an-input-checkbox-that-is-disabled
$(document).on('click', 'input[type="checkbox"][readonly]', function(e){
e.preventDefault();
});
})();
CSS:
input[type="checkbox"][readonly] {
cursor: not-allowed;
opacity: 0.5;
}
Since the element is not 'disabled' it still goes through the POST.
<html>
<head>
<script type="text/javascript">
function some_name() {if (document.getElementById('first').checked) document.getElementById('second').checked=false; else document.getElementById('second').checked=true;}
</script>
</head>
<body onload="some_name();">
<form>
<input type="checkbox" id="first" value="first" onclick="some_name();"/>first<br/>
<input type="checkbox" id="second" value="second" onclick="some_name();" />second
</form>
</body>
</html>
Function some_name is an example of a previous clause to manage the second checkbox which is checked (posts its value) or unchecked (does not post its value) according to the clause and the user cannot modify its status; there is no need to manage any disabling of second checkbox
Here is another work around can be controlled from the backend.
ASPX
<asp:CheckBox ID="Parts_Return_Yes" runat="server" AutoPostBack="false" />
In ASPX.CS
Parts_Return_Yes.InputAttributes["disabled"] = "disabled";
Parts_Return_Yes.InputAttributes["class"] = "disabled-checkbox";
Class is only added for style purposes.
© 2022 - 2024 — McMap. All rights reserved.