【问题标题】:jquery ui autocomplete autofocus with category element as firstjquery ui自动完成自动对焦,类别元素为第一
【发布时间】:2015-11-09 12:56:16
【问题描述】:

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

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

我已经尝试过的: 1. 将 ui-state-disabled 添加到我的类别 2. 搜索谷歌如何覆盖 menu.next() 函数,以便我可以检测项目是否为类别 3. google了如何手动设置选中元素,包括触发select事件

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

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

【问题讨论】:

  • 你能上传一些代码吗?还是 jsfiddle?
  • 您可以使用open 上的所需字段触发select 事件。不过,这不会禁用格式不正确的项目:stackoverflow.com/questions/7633727/…
  • 添加了 jsfidle 来显示问题,因为您可以看到类别是可选择的,并且在 jquery 1.11 中也可以通过 autoFocus 选择。如果我使用 jquery 1.9,它会像我想要的那样工作,类别不可选择并且 autofocus = 选择第一个真实项目

标签: javascript jquery jquery-ui autocomplete


【解决方案1】:

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

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

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

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

【讨论】:

  • 如果小提琴是我的问题的真实代表,那将是正确的。目前的问题是,在 jquery 1.11 中,类别 = 是可选的,而在 jquery 1.10 中,它们不是...
【解决方案2】:

找到:jquery ui autocomplete combobox with categories

回答者:Aureltime

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-10
    • 2011-04-23
    • 2017-08-06
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多