简体   繁体   English

组 <select>价值不存在的选择

[英]Set <select> value to a non-existent option

I've got a select with options. 我有选择的选择。 I need to set the default of the dropdown to a value that is not in the dropdown list. 我需要将下拉菜单的默认值设置为不在下拉列表中的值。 The user can then leave it the same or select a value from the options in the select. 然后,用户可以将其保留不变,也可以从选择中的选项中选择一个值。

How can this be achieved? 如何做到这一点?

Edit: This is the scenario: I have a set of data that is normalized. 编辑:这是方案:我有一组标准化的数据。 This is fed into the select (as options). 这被输入到选择中(作为选项)。 After rendering, a value is selected by default. 渲染后,默认情况下会选择一个值。 However, sometimes, this value (that has to be selected) might not be in the options - so a good UX would be to still have this shown as the selected option, even if it is not in the normalized data. 但是,有时,此值(必须选择)可能不在选项中-因此,一个好的UX将仍然将此值显示为所选选项,即使它不在规范化数据中也是如此。 The user can leave it be or select a "correct" option. 用户可以保留原样或选择“正确”选项。

I assume you mean for a placeholder similar to text inputs. 我假设您的意思是类似于文本输入的占位符。 The only way I know of to do this is to make the selected option disabled; 我唯一知道的方法是禁用选定的选项。 however, it must still be an option in the markup. 但是,它仍然必须在标记的选项。 eg 例如

 <select> <option disabled="" selected="" value="">Select one of the Options Below</option> <option>Option 1</option> <option>Option 2</option> <option>Option 3</option> </select> 

You may be better served by making your own javascript powered one. 制作自己的JavaScript技术可能会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM