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

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

问题描述

我有一个 select 表单字段,我想标记为readonly,因为在用户不能修改的值,但值仍然提交与形式。使用 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 属性仅适用于输入 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 ,禁用所有选项,并使用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 通常给予< select>

另一个选项。

<select name="myselect" disabled="disabled">
    <option value="myselectedvalue" selected="selected">My Value</option>
    ....
</select>
<input type="hidden" name="myselect" value="myselectedvalue" />

现在有了这个,我注意到,根据你使用的网络服务器,在< select> 之前或之后输入隐藏

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天全站免登陆