jquery - typeahead.js 返回 "blank"数据集

标签 jquery autocomplete typeahead.js

我的 Twitter 有点问题 typeAhead返回空白结果和官方文档也没有帮助。

我期待什么:

<div class="tt-suggestion tt-selectable">Warcraft II: Tides of Darkness</div>
...

发生了什么:

在输入字段中输入时,typeAhead 返回空 html,如下所示,列表会根据获取的结果数量进行填充,因此它正在工作

<div class="tt-suggestion tt-selectable"></div>
<div class="tt-suggestion tt-selectable"></div>
<div class="tt-suggestion tt-selectable"></div>
<div class="tt-suggestion tt-selectable"></div>
<div class="tt-suggestion tt-selectable"></div>

我的代码:

var gameTitles= new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  remote: {
    url: '../api/autocomplete/%QUERY',
    wildcard: '%QUERY'
  }
});

$('#title').typeahead({minLength: 3}, {
  name: 'game-titles',
  display: 'value',
  source: gameTitles
});

作为引用,远程返回以下内容,因此我确定我的查询已正确传递

[
  "Warcraft II: Tides of Darkness",
  "Warcraft II: Beyond the Dark Portal",
  "Warcraft III: Reign of Chaos",
  "Warcraft II: The Dark Saga",
  "Peggle: World of Warcraft Edition",
  "Warcraft III: Reforged Spoils of War Edition",
  "World of Warcraft",
  "Warcraft III: Reforged",
  "Warcraft: Orcs & Humans",
  "World of Warcraft: Legion"
]

最佳答案

Typeahead 会查找数组中的对象,默认情况下,建议会在这些对象中查找 value,这可以通过将您希望使用的属性名称分配给 显示属性

$('#title').typeahead({}, {
  name: 'game-titles',
  display: 'WHATEVER PROPERTY YOU WISH DISPLAYED',
  source: gameTitles
});

此外,typeAhead 不再维护,selectize.js 似乎是一个不错的替代方案

关于jquery - typeahead.js 返回 "blank"数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54541705/

相关文章:

Javascript 在正文标签中添加新类

jQuery 获取 html 元素的属性

c++ - Vim 中的自动补全

javascript - 无法在 typeahead js 中设置与 displaykey 不同的 valuekey

javascript - 在多个字段中进行 Twitter Bootstrap 的 typead 搜索

jquery - 我需要一些帮助来居中和获取内联文本和图像 (Jquery)

javascript - # jquery 移动页面 url 中的字符

R:在自定义类中启用自动完成

javascript - 自动完成在我的 asp.net mvc 中不显示任何 CSS

javascript - 使用vuejs隐藏和显示后jquery方法停止工作