How can I get the form value from a disabled <input> element

后端 未结 10 1379
小鲜肉
小鲜肉 2020-12-04 23:50

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?

相关标签:
10条回答
  • 2020-12-05 00:10

    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"/>
    
    0 讨论(0)
  • 2020-12-05 00:14

    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

    0 讨论(0)
  • 2020-12-05 00:15

    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">
    
    0 讨论(0)
  • 2020-12-05 00:17

    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.

    0 讨论(0)
  • 2020-12-05 00:28

    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?

    0 讨论(0)
  • 2020-12-05 00:29

    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.

    0 讨论(0)
提交回复
热议问题