带有值的 jQuery UI 自动完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4866699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:04:03  来源:igfitidea点击:

jQuery UI Autocomplete with values

jqueryautocomplete

提问by PNR

Is it somehow possible to set a value (value/text as in a combobox) in jQuery UI Autocomplete?

是否可以在 jQuery UI 自动完成中设置一个值(如组合框中的值/文本)?

I need to get a DB id from the selected item in the Autocomplete box.

我需要从自动完成框中的选定项目中获取数据库 ID。

Thanks in advance!

提前致谢!

回答by David Hoerster

You really can't, at least as far as I've seen, set a value in the jQueryUI autocomplete widget, but you can use the widget to set the value of an attribute in a hidden field or maybe an attribute on some other element.

你真的不能,至少就我所见,在 jQueryUI 自动完成小部件中设置一个值,但你可以使用小部件来设置隐藏字段中的属性值,或者其他元素上的属性值.

Here's a quick jsFiddleI put together to demostrate it. Hopefully this can help.

这是我用来演示它的一个快速jsFiddle。希望这会有所帮助。

My example is based on the jQueryUI AutoComplete example for "Custom Data and Display". Basically, it assumes you get some array of data back from a service or it's rendered from the server-side. In your array of data, you have a display field, a key value and your DB ID. The key value really isn't necessary. But when you select an item in your autocomplete list, you can capture what was selected in the autocomplete's selectevent and then handle the data appropriately. I stored the 'id' value in my array in a hidden field, but you can handle it differently.

我的示例基于“自定义数据和显示”的 jQueryUI AutoComplete 示例。基本上,它假设您从服务获取一些数据数组,或者它是从服务器端呈现的。在您的数据数组中,您有一个显示字段、一个键值和您的数据库 ID。键值真的没有必要。但是当您在自动完成列表中选择一个项目时,您可以捕获在自动完成select事件中选择的内容,然后适当地处理数据。我将数组中的 'id' 值存储在一个隐藏字段中,但您可以以不同的方式处理它。

Hope this helps!

希望这可以帮助!