49

The HTML standard for forms appears to be such that disabled input elements do not contribute to the form name/value collection.

Is there ANY way to get around this? I need to be able to toggle disable on and off but still return whatever the value is when the form is submitted.

I realize that I can use JavaScript to copy the value to a hidden input before the form is submitted, but I would prefer not to. Is there a cleaner way?

I'm using ASP.NET, not that that matters.

Samuel
  • 37,778
  • 11
  • 85
  • 87

11 Answers11

114

You should use the readOnly flag rather than disabled. Read-only fields cannot be edited by the user, but are still submitted with the form.

<input type="text" value="blah" readonly="true"/>
JosephGarrone
  • 4,081
  • 3
  • 38
  • 61
levik
  • 114,835
  • 27
  • 73
  • 90
24

The HTML standard for forms appears to be such that disabled input elements do not contribute to the form name/value collection.

That is correct.

HACK: You could use Javascript to, upon submit:

  1. Unset disabled
  2. Set readonly
  3. Submit!
Wayne
  • 1,288
  • 1
  • 7
  • 20
  • Brilliant, thanks. If I have to use Javascript, I'd prefer not to copy values around. This works in IE and FF at least. I'm still testing cross browser, but I'll give you the answer flag for now. –  Feb 07 '09 at 18:07
  • You could also use JavaScript to, on submit, add hidden input fields to the form for all the disabled elements and copy their values. – Miles Mar 24 '09 at 22:15
9

If you make the value readonly, instead of disabling it, the field's name/value will be sent with the rest of the non-disabled fields.

Make the readonly fields' focus event handler pass the focus to the next eligible field, to make it act more like a disabled element. Some browsers let you focus and select readonly fields, and some even let you paste into a readonly field, though they revert to the original value onblur and onchange.

<input type="text" value="" readonly="readonly">
kennebec
  • 102,654
  • 32
  • 106
  • 127
8

As a slightly more robust variant of Wayne's hack (which might get confused by a Back button push), when disabling a control: set readonly= true and className= 'disabled' instead of disabled= true, then style .disabled to look similar to a disabled field.

bobince
  • 528,062
  • 107
  • 651
  • 834
2

I whipped up a quick (Jquery only) plugin, that saves the value in a data field while an input is disabled. This just means as long as the field is being disabled programmaticly through jquery using .prop() or .attr()... then accessing the value by .val(), .serialize() or .serializeArra() will always return the value even if disabled :)

https://github.com/Jezternz/jq-disabled-inputs

Josh Mc
  • 9,911
  • 8
  • 53
  • 66
1

You can easily

  1. Change status to normal (disabled=false)
  2. Read Value
  3. reset input status to disabled

    $(element).prop("disabled", false); var text = $(element).val(); $(element).prop("disabled", true)
    
Mehdi Saghari
  • 154
  • 1
  • 9
0

A little variant,set readonly flag and then with css add this to the input element

.read_only_input {
background: #efefef; /* to look like disabled */
pointer-events: none; /* to avoid focus on input */

}

DR-MATTH
  • 121
  • 3
  • 5
-1

Its Simple only two steps

  1. check if the input you want to access is disabled if it is then remove its "disabled" attribute

  2. Get the value and then add the disabled attribute again.

Mateen
  • 1,631
  • 1
  • 23
  • 27
-1

I suppose this is a hack, but seems to work well for me. Do both! User sees disabled input, clearly ghosted and browser prevents interaction - but it is not submitted with form. The user doesn't see the potentially disorienting readonly field, yet it is submitted with the form.

<input name="mode_d" size="8" value="mode" disabled>
<input name="mode"            value="mode" hidden readonly>
rickatech
  • 551
  • 4
  • 10
-1

Can you use Visible=false and/or ReadOnly=true instead of Enabled=false?

If you are using the control, you shouldn't really set Enabled=false?

Ian G
  • 29,468
  • 21
  • 78
  • 92
  • I'm not using the Enabled property that ASP.NET provides, just setting disabled "disabled" or not with JavaScript. Thanks for the contribution. –  Feb 07 '09 at 18:11
  • Setting Visible=False means the control won't even render to the page. There would be nothing to even check for input. – womp Mar 24 '09 at 22:01
-1

Instead of setting field as disable set readonly attribute to "readonly " like shown below.

readonly=readonly

this will send your field value in form submit.

Liam
  • 27,717
  • 28
  • 128
  • 190