179

Is there any way (with a attribute flag or something like that) to enable form fields that are disabled to submit data?

Or, if that's not possible, is there any way to block fields from editing with css or any other attribute than disabled without hiding them?

My special case at the moment is an identifier for a data-set that should be shown in the form (uneditable) - if there is no better solution I think i'll use a hidden field in addition to the disabled one to hold the actual value with the disabled one showing it.

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
bardiir
  • 14,556
  • 9
  • 41
  • 66
  • 2
    Why not use "readonly" instead of "disabled"? – DevWL Nov 01 '20 at 02:51
  • Duplicate the field in question. have a input with type hidden filled with the value you need to submit. Since you have your disabled field it doesn't change. When you fill one fill both. – Bruno May 12 '23 at 10:25

4 Answers4

56

As it was already mentioned: READONLY does not work for <input type='checkbox'> and <select>...</select>.

If you have a Form with disabled checkboxes / selects AND need them to be submitted, you can use jQuery:

$('form').submit(function(e) {
    $(':disabled').each(function(e) {
        $(this).removeAttr('disabled');
    })
});

This code removes the disabled attribute from all elements on submit.

Mario Werner
  • 1,771
  • 14
  • 24
23

Use the CSS pointer-events:none on fields you want to "disable" (possibly together with a greyed background) which allows the POST action, like:

<input type="text" class="disable">

.disable{
pointer-events:none;
background:grey;
}

Ref: https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events

Tomasz
  • 1,288
  • 18
  • 36
4

We can also use the readonly only with below attributes -

readonly onclick='return false;'

This is because if we will only use the readonly then radio buttons will be editable. To avoid this situation we can use readonly with above combination. It will restrict the editing and element's values will also passed during form submission.

Kripa Yadav
  • 111
  • 1
  • 3
  • Use of `readonly` instead of `disabled` attribute to keep `` data is such an elegant solution! – Nik Mar 31 '23 at 15:31
0

add CSS or class to the input element which works in select and text tags like

style="pointer-events: none;background-color:#E9ECEF"

Paresh
  • 77
  • 1
  • 6