如何从禁用的元素获取表单值

时间:2022-12-10 16:00:49

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

表单的HTML标准似乎是禁用的输入元素不对表单名称/值集合做出贡献。

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?

我意识到我可以使用JavaScript在提交表单之前将值复制到隐藏的输入,但我不愿意。有更干净的方式吗?

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

我正在使用ASP.NET,而不是那么重要。

9 个解决方案

#1


77  

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.

您应该使用readOnly标志而不是禁用。用户无法编辑只读字段,但仍会随表单一起提交。

<input type="text" value="blah" readonly="true"/>

#2


19  

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

表单的HTML标准似乎是禁用的输入元素不对表单名称/值集合做出贡献。

That is correct.

那是对的。

HACK: You could use Javascript to, upon submit:

HACK:您可以在提交时使用Javascript:

  1. Unset disabled
  2. 取消设置已禁用
  3. Set readonly
  4. 只读设置
  5. Submit!
  6. 提交!

#3


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.

使readonly字段的焦点事件处理程序将焦点传递给下一个符合条件的字段,使其更像是一个禁用的元素。有些浏览器可以让你专注并选择只读字段,有些浏览器甚至可以让你粘贴到只读字段,尽管它们会恢复到原始值onblur和onchange。

<input type="text" value="" readonly="readonly">

#4


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.

作为Wayne骇客的一个稍微强大的变种(可能会被后退按钮推动混淆),当禁用控件时:设置readonly = true而className ='disabled'而不是disabled = true,则样式.disabled看起来类似于禁用的字段。

#5


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 :)

我掀起了一个快速(仅限Jquery)插件,它在输入被禁用时保存数据字段中的值。这只是意味着只要通过使用.prop()或.attr()的jquery以编程方式禁用该字段...然后通过.val(),. serialize()或.serializeArra()访问该值将始终返回即使禁用了:)

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

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

#6


0  

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.

    获取值,然后再次添加disabled属性。

#7


0  

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>

#8


-1  

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

你能使用Visible = false和/或ReadOnly = true而不是Enabled = false吗?

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

如果您正在使用该控件,您不应该真的设置Enabled = false?

#9


-1  

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

而不是将字段设置为禁用设置readonly属性为“readonly”,如下所示。

readonly=readonly

this will send your field value in form submit.

这将在表单提交中发送您的字段值。

#1


77  

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.

您应该使用readOnly标志而不是禁用。用户无法编辑只读字段,但仍会随表单一起提交。

<input type="text" value="blah" readonly="true"/>

#2


19  

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

表单的HTML标准似乎是禁用的输入元素不对表单名称/值集合做出贡献。

That is correct.

那是对的。

HACK: You could use Javascript to, upon submit:

HACK:您可以在提交时使用Javascript:

  1. Unset disabled
  2. 取消设置已禁用
  3. Set readonly
  4. 只读设置
  5. Submit!
  6. 提交!

#3


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.

使readonly字段的焦点事件处理程序将焦点传递给下一个符合条件的字段,使其更像是一个禁用的元素。有些浏览器可以让你专注并选择只读字段,有些浏览器甚至可以让你粘贴到只读字段,尽管它们会恢复到原始值onblur和onchange。

<input type="text" value="" readonly="readonly">

#4


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.

作为Wayne骇客的一个稍微强大的变种(可能会被后退按钮推动混淆),当禁用控件时:设置readonly = true而className ='disabled'而不是disabled = true,则样式.disabled看起来类似于禁用的字段。

#5


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 :)

我掀起了一个快速(仅限Jquery)插件,它在输入被禁用时保存数据字段中的值。这只是意味着只要通过使用.prop()或.attr()的jquery以编程方式禁用该字段...然后通过.val(),. serialize()或.serializeArra()访问该值将始终返回即使禁用了:)

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

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

#6


0  

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.

    获取值,然后再次添加disabled属性。

#7


0  

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>

#8


-1  

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

你能使用Visible = false和/或ReadOnly = true而不是Enabled = false吗?

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

如果您正在使用该控件,您不应该真的设置Enabled = false?

#9


-1  

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

而不是将字段设置为禁用设置readonly属性为“readonly”,如下所示。

readonly=readonly

this will send your field value in form submit.

这将在表单提交中发送您的字段值。