jquery-ui自动完成自动对焦,首先使用category元素

jquery ui autocomplete autofocus with category element as first

本文关键字:元素 category jquery-ui      更新时间:2023-09-26

我正试图在使用类别时选择jquery自动完成中的第一个项目。现在我的菜单看起来像这样:

[category] <- autofocus selects this one
[item1]    <- autofocus should select this one
[item2]
[item3]
[category]
[item4]
[item5]
etc.

我已经尝试过的:1.将禁用ui状态添加到我的类别2.在谷歌上搜索了如何覆盖menu.next()函数,这样我就可以检测该项目是否是一个类别3.在谷歌上搜索了如何手动设置所选元素,包括触发选择事件

到目前为止,我没有运气,所以我希望你们有任何建议^^

fidle:http://jsfiddle.net/uynct53p/5/(适用于jquery 1.9,但不适用于1.11)

由于它仍然是一个输入元素,您可以在自动完成时调用val方法来设置其值。

由于您想将值设置为data对象中的第一个元素,因此只需执行

$( "#search" ).autocomplete({ /* ... */ })
.val(data[0].label)
// ...

你的小提琴:http://jsfiddle.net/uynct53p/7/

找到:jquery ui自动完成组合框,包含类别

回答者:Auretime

这使得jquery 1.11.*中的类别不可选择

$.extend($.ui.menu.prototype.options, {
    items: "> :not(.ui-autocomplete-category)"
});