禁用的表单输入不会出现在请求中 [英] Disabled form inputs do not appear in the request
问题描述
我在表单中有一些禁用的输入,我想将它们发送到服务器,但 Chrome 将它们从请求中排除.
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>
推荐答案
具有 disabled
属性的元素未提交,或者您可以说它们的值未发布(请参阅步骤下的第二个要点;HTML 5 规范中用于构建表单数据的 3设置).
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).
即,
<input type="textbox" name="Percentage" value="100" disabled="disabled" />
仅供参考,根据 17.12.1 在 HTML 4 规范中:
FYI, per 17.12.1 in the HTML 4 spec:
- 禁用的控件不会获得焦点.
- 在标签导航中跳过禁用的控件.
- 无法成功发布禁用的控件.
您可以在您的案例中使用 readonly
属性,这样您就可以发布您的字段数据.
You can use readonly
attribute in your case, by doing this you will be able to post your field's data.
即,
<input type="textbox" name="Percentage" value="100" readonly="readonly" />
仅供参考,根据 17.12.2 在 HTML 4 规范中:
FYI, per 17.12.2 in the HTML 4 spec:
- 只读元素获得焦点但不能被用户修改.
- 只读元素包含在标签导航中.
- 只读元素已成功发布.
这篇关于禁用的表单输入不会出现在请求中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!