简体   繁体   English

如何使用“新值”选项创建下拉列表?

[英]How to create dropdown list with “new value” option?

I would like to create a dropdown list where the last option will be to enter a new value. 我想创建一个下拉列表,最后一个选项是输入新值。 How could I create this ? 我该如何创建呢?

Is there any jQuery plugin that provides such thing ? 有没有提供此类功能的jQuery插件?

There is no Html element that can do what you are looking for natively. 没有Html元素可以本地完成您要寻找的功能。

However as a workaround, you can bind a change event handler to your dropdown list. 但是,作为解决方法,您可以将更改事件处理程序绑定到下拉列表。 The rest depends on how exactly you would like the user to enter the new value: 其余的取决于您希望用户输入新值的精确程度:

$("#theList").change(function (e) {
                    //open a thickbox asking for the value, convert the dropdown list to a regular input or whatever...
                });

Note that some browsers raise an onchange event when the listbox is changed, others only when it's change and loses focus. 请注意,某些浏览器在更改列表框时引发onchange事件,其他浏览器仅在更改列表框并失去焦点时才触发。 So you might also want to listen to mousedown and keydown events if you want your UI to provide a consistent behavior. 因此,如果您希望用户界面提供一致的行为,则可能还需要监听mousedown和keydown事件。

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

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