禁用的表单输入不会出现在请求中
我在表单中有一些禁用的输入,我想将它们发送到服务器,但 Chrome 将它们从请求中排除。
有没有办法在不添加隐藏字段的情况下解决此问题?
<form action="/Media/Add">
<input type="hidden" name="Id" value="123" />
<!-- this does not appear in request -->
<input type="textbox" name="Percentage" value="100" disabled="disabled" />
</form>
I have some disabled inputs in a form and I want to send them to a server, but Chrome excludes them from the request.
Is there any workaround for this without adding a hidden field?
<form action="/Media/Add">
<input type="hidden" name="Id" value="123" />
<!-- this does not appear in request -->
<input type="textbox" name="Percentage" value="100" disabled="disabled" />
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
具有
disabled
属性的元素不会提交,或者您可以说它们的值未发布(请参阅 用于构建表单数据集的 HTML 5 规范)。即,
仅供参考,根据 17.12.1< HTML 4 规范中的 /a>:
您可以在您的情况下使用
readonly
属性,通过这样做,您将能够发布字段的数据。即,
仅供参考,根据 17.12.2< HTML 4 规范中的 /a>:
Elements with the
disabled
attribute are not submitted or you can say their values are not posted (see the second bullet point under Step 3 in the HTML 5 spec for building the form data set).I.e.,
FYI, per 17.12.1 in the HTML 4 spec:
You can use
readonly
attribute in your case, by doing this you will be able to post your field's data.I.e.,
FYI, per 17.12.2 in the HTML 4 spec:
使用 Jquery 并使用 ajax 发送数据,您可以解决您的问题:
Using Jquery and sending the data with ajax, you can solve your problem:
除了启用输入之外,要发布来自禁用输入的值,您只需在提交表单时重新启用所有表单输入即可。
如果您更喜欢 jQuery:
对于 ASP.NET MVC C# Razor,您可以像这样添加提交处理程序:
To post values from disabled inputs in addition to enabled inputs, you can simply re-enable all of the form's inputs as it is being submitted.
If you prefer jQuery:
For ASP.NET MVC C# Razor, you add the submit handler like this:
如果您绝对必须禁用该字段并传递数据,您可以使用 javascript 将相同的数据输入到隐藏字段(或者也只设置隐藏字段)。这将允许您禁用它,但即使您要发布到另一个页面,仍然发布数据。
If you absolutely have to have the field disabled and pass the data you could use a javascript to input the same data into a hidden field (or just set the hidden field too). This would allow you to have it disabled but still post the data even though you'd be posting to another page.
我正在更新这个答案,因为它非常有用。只需将 readonly 添加到输入即可。
所以形式将是:
I'm updating this answer since is very useful. Just add readonly to the input.
So the form will be:
从语义上讲,这感觉像是正确的行为,
我会问自己“为什么我需要提交这个值?”
如果您在表单上禁用了输入,那么您可能不希望用户更改直接值
禁用输入中显示的任何值都应该
假设处理表单的服务器是同一个作为服务它的服务器,用于重现禁用输入值的所有信息都应该在处理时可用
事实上,以保持数据完整性 - 即使禁用输入的值已发送到处理服务器,你真的应该验证它。 此验证需要的信息级别与您重现值所需的信息级别相同!
我几乎认为只读输入不应在请求要么
很高兴被纠正,但是所有用例我能想到需要提交只读/禁用输入的地方实际上只是变相的样式问题
Semantically this feels like the correct behaviour
I'd be asking myself "Why do I need to submit this value?"
If you have a disabled input on a form, then presumably you do not want the user changing the value directly
Any value that is displayed in a disabled input should either be
Assuming that the server processing the form is the same as the server serving it, all the information to reproduce the values of the disabled inputs should be available at processing
In fact, to preserve data integrity - even if the value of the disabled input was sent to the processing server, you should really be validating it. This validation would require the same level of information as you would need to reproduce the values anyway!
I'd almost argue that read-only inputs shouldn't be sent in the request either
Happy to be corrected, but all the use cases I can think of where read-only/disabled inputs need to be submitted are really just styling issues in disguise
我发现这更容易操作。只读输入字段,然后设置其样式,以便最终用户知道它是只读的。放置在这里的输入(例如来自 AJAX 的输入)仍然可以提交,无需额外的代码。
I find this works easier. readonly the input field, then style it so the end user knows it's read only. inputs placed here (from AJAX for example) can still submit, without extra code.
简单的解决方法 - 只需使用隐藏字段作为选择、复选框和单选的占位符。
从这段代码到 -
那个代码 -
Simple workaround - just use hidden field as placeholder for select, checkbox and radio.
From this code to -
that code -
使用适当的样式使其只读:
Make it readonly with proper style:
除了 Tom Blodget 的响应之外,您还可以简单地添加 @HtmlBeginForm 作为表单操作,如下所示:
In addition to Tom Blodget's response, you may simply add @HtmlBeginForm as the form action, like this:
使用
use
使用 RGBA 值定义颜色
在 style 下添加以下代码
Disabled Grey none tranparent
结果
Define Colors With RGBA Values
Add the Following code under style
Disabled Grey none tranparent
result
我遇到了完全相同的问题,但对我不起作用,因为我选择了 HTML 元素,并且它的只读状态允许更改其值。
所以我在一种条件下使用 select 并在另一种条件下输入:
I had exactly the same problem, but did not work for me, because I have select HTML element, and it's read-only state allowed to change its value.
So I used select in one condition and input in another:
您可以完全避免禁用,这很痛苦,因为 html 表单格式不会发送与
或其他标签相关的任何内容。
所以你可以把常规的
添加这个
readonly="readonly"
它不会禁用您的文本,但也不会由用户更改,因此它的工作方式类似于
并将通过表单发送值。如果您想让它更像
标签,只需删除边框即可
You can totally avoid disabling, it is painful since html form format won't send anything related to
<p>
or some other label.So you can instead put regular
add this
readonly="readonly"
It wouldn't disable your text but wouldn't change by user so it work like
<p>
and will send value through form. Just remove border if you would like to make it more like<p>
tag