I have a form with input text; I change this input dynamically with JQuery
to disabled - but sometimes I put something in this input and I disable it. When I send this form I don't have this input with my data
How can I get the value of this input even if it is disabled:
<input type="text" id="myInput" name="myInput"/>
JQuery :
$('#Checkbox').change(function() {
if ($('#myInput').prop("disabled")) {
$("#myInput").prop('disabled', false);
} else {
$("#myInput").prop('disabled', true);
}
});
A readonly
element is just not editable, but gets sent when the according form submits. a disabled
element isn't editable and isn't sent on submit.
So instead of making it disabled
change it to readonly
$('#Checkbox').change(function() {
if($('#myInput').prop("readonly")) {
$( "#myInput" ).prop('readonly', false);
} else{
$( "#myInput" ).prop('readonly', true);
}
});
You can do this by adding a hidden field underneath with the value you want to POST.
<input type="text" id="myInput" name="myInput"/>
<input type="hidden" id="myInputHidden" name="myInput"/>
then
$('#Checkbox').change(function() {
$("#myInputHidden").val($("#myInput").val());
if ($('#myInput').prop("disabled")) {
$( "#myInput" ).prop('disabled', false);
}
else {
$( "#myInput" ).prop('disabled', true);
}
});
When the form POSTs, the value for 'myInput' will be the text field if it's enabled and the hidden field if the text field is disabled.