如何确保<select>表单字段在禁用时提交? [英] How to ensure a <select> form field is submitted when it is disabled?

查看:19
本文介绍了如何确保<select>表单字段在禁用时提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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 属性仅适用于 inputtextarea 字段,但这基本上是我想要的.有什么办法可以让它工作吗?

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 the options 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 是您通常为 之前或之后代码>.

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.

这篇关于如何确保&lt;select&gt;表单字段在禁用时提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆