[英]How to get selected value from a dropdown after it has been disabled after a selection
I have this code below. 我下面有这段代码。 Everything will be disabled after a selection.
选择后将禁用所有功能。
<select id="dropdown" name="dropdown" onchange="javascript:this.disabled = 'disabled';">
<option value="dropdown">Pls select one
<option value="apple">Apple
<option value="oragne">Orange
<option value="grapes">Grapes
</select>
But when I hit submit, the value for the "dropdown" is not being passed. 但是当我点击提交时,“下拉列表”的值没有被传递。 Am I missing something here?
我在这里想念什么吗?
Disabled form controls don't get submitted. 禁用的表单控件不会被提交。 This is normal behaviour.
这是正常的行为。
One work around is to copy the selected value to a hidden input that would get submitted. 一种解决方法是将所选值复制到将要提交的隐藏输入中。
(I really don't recommend disabling a select on change though - what if the user accidentally clicked the wrong thing? What if they were trying to select with the keyboard?) (尽管如此,我确实不建议禁用更改选择-如果用户不小心单击了错误的内容该怎么办?如果他们尝试使用键盘进行选择该怎么办?)
(And as another aside, you don't need javascript:
in inline event attributes.) (此外,您不需要
javascript:
内联事件属性。)
EDIT: How to implement the hidden input? 编辑:如何实现隐藏的输入? Well, give the hidden input the name that your server-side code expects (and remove the
name
attribute from the select element): 好吧,为隐藏的输入提供服务器端代码期望的
name
(并从select元素中删除name
属性):
<input type="hidden" name="dropdown" id="dropdown">
And then your onchange
attribute would be something like: 然后,您的
onchange
属性将类似于:
<select onchange="document.getElementById('dropdown').value=this.value; this.disabled=true;">
The value of your selected option from your dropdown will not be passed its because you had your dropdown disabled. 从下拉菜单中选择的选项的值将不会传递,因为您已禁用下拉菜单。 maybe thats the reason why.
也许那就是原因。 try not disabling it.
尝试不要禁用它。
It seems disabled selection does not submit by default. 似乎默认情况下不选择禁用。
You can add another hidden selection element. 您可以添加另一个隐藏的选择元素。 And use script to make choice to submit.
并使用脚本进行选择提交。
It doesn't really make sense to disable a control, then submit its value. 禁用控件然后提交其值实际上没有任何意义。 If you want the value submitted, don't disable it.
如果要提交值,请不要禁用它。
Anyhow, one option is to enable the control before submitting: 无论如何,一种选择是在提交之前启用控件:
<form ... onsubmit="this.dropdown.disabled = false;">
Now you don't have to copy the value anywhere and the value will be submitted whether the control is disabled or not. 现在,您不必将值复制到任何地方,无论是否禁用控件,都将提交该值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.