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

后端 未结 10 1380
小鲜肉
小鲜肉 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:30

    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.

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

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

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

    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>
    
    0 讨论(0)
提交回复
热议问题