How can I get the form value from a disabled <i

2019-01-17 00:16发布

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.

9条回答
Summer. ? 凉城
2楼-- · 2019-01-17 00:24

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.

查看更多
迷人小祖宗
3楼-- · 2019-01-17 00:25

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.

查看更多
够拽才男人
4楼-- · 2019-01-17 00:28

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">
查看更多
叛逆
5楼-- · 2019-01-17 00:30

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?

查看更多
我命由我不由天
6楼-- · 2019-01-17 00:32

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!
查看更多
该账号已被封号
7楼-- · 2019-01-17 00:32

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

查看更多
登录 后发表回答