简体   繁体   English

选择后禁用下拉菜单后如何从下拉列表中获取所选值

[英]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.

相关问题 选择下拉菜单后如何向其添加禁用验证 - How to add disable validation to a dropdown after it has been selected 使用基因剔除进行选择后,从下拉菜单中设置所选值的“标题” - Set “title” of selected value from dropdown after selection using knockout 选择烧瓶后未显示下拉选择的值? - Dropdown selected value is not showing after selection flask? 单击按钮后如何从按钮中获取值 - How to get value from button after it has been clicked 选择后,如何阻止我的javascript下拉列表显示空白框而不是所选值? - How can I stop my javascript dropdown from displaying blank box instead of selected value after selection? 删除已从另一个下拉菜单中选择的下拉值 - Remove a dropdown value that has been selected from another dropdown menu 从下一个下拉菜单中删除已选择的下拉值 - Remove a dropdown value that has been selected, from next dropdown menus 尝试获取已通过下拉选择项更新的字段的文本值 - Trying to get the text value of a field that has been updated by a dropdown selection 如何检查是否<inputb />在一个值之后有一个值<inputa />已被选中? - How to check if <InputB /> has a value after a value for <InputA /> has been selected? jQuery修改后如何获取输入值 - How to get input value after it has been changed by jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM