如何确保<select>表单字段在禁用时提交? [英] How to ensure a <select> form field is submitted when it is disabled?
问题描述
我有一个 select
表单字段,我想将其标记为只读",因为用户无法修改该值,但该值仍随表单一起提交.使用 disabled
属性可防止用户更改值,但不会随表单提交值.
I have a select
form field that I want to mark as "readonly", as in the user cannot modify the value, but the value is still submitted with the form. Using the disabled
attribute prevents the user from changing the value, but does not submit the value with the form.
readonly
属性仅适用于 input
和 textarea
字段,但这基本上是我想要的.有什么办法可以让它工作吗?
The readonly
attribute is only available for input
and textarea
fields, but that's basically what I want. Is there any way to get that working?
我正在考虑的两种可能性包括:
Two possibilities I'm considering include:
- 不是禁用
select
,而是禁用所有option
并使用 CSS 使选择变灰,使其看起来像已禁用. - 向提交按钮添加一个点击事件处理程序,以便在提交表单之前启用所有禁用的下拉菜单.
- Instead of disabling the
select
, disable all of theoption
s and use CSS to gray out the select so it looks like its disabled. - Add a click event handler to the submit button so that it enables all of the disabled dropdown menus before submitting the form.
推荐答案
<select disabled="disabled">
....
</select>
<input type="hidden" name="select_name" value="selected value" />
其中 select_name
是您通常为 指定的名称.
Where select_name
is the name that you would normally give the <select>
.
另一种选择.
<select name="myselect" disabled="disabled">
<option value="myselectedvalue" selected="selected">My Value</option>
....
</select>
<input type="hidden" name="myselect" value="myselectedvalue" />
现在有了这个,我注意到根据您使用的网络服务器,您可能需要将 hidden
输入放在 之前或之后代码>.
Now with this one, I have noticed that depending on what webserver you are using, you may have to put the hidden
input either before, or after the <select>
.
如果我没记错的话,IIS 放在前面,Apache 放在后面.一如既往,测试是关键.
If my memory serves me correctly, with IIS, you put it before, with Apache you put it after. As always, testing is key.
这篇关于如何确保<select>表单字段在禁用时提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!