javascript - typeahead js不显示结果

标签 javascript typeahead.js

我想使用 typeahead 远程检索邮政编码,并且必须是 post,而不是 get。该调用在控制台中返回以下 json:

{suggestions: "L-6956 | IM GRUND | KEEN-SUR-DOHEEM"}
{suggestions: "L-6956 | OP DER MOUCK | KEEN-SUR-DOHEEM"}

但是结果不会显示在输入字段下以便选择结果之一。这是我的代码:

$('#txtPostalCode').typeahead(
  null,
  {
    name: 'txtPostalCode',
    displayKey: 'suggestions',
    minLength: 3,
    source: function (query, syncResults) {
      $.post('/hotbed/sggl/getpipedaddresses', {searchItem: query}, function (data) {
        syncResults($.map(data, function (item) {
          console.log(item.suggestions);
          return item;
        }));
      }, 'json');

    }
  });

最佳答案

根据 typeahead API,服务器响应应标记为异步,并且应使用该 asyncCB 获取您的响应,

$('#txtPostalCode').typeahead({
  null
},
{
  name: 'txtPostalCode',
  displayKey: 'suggestions',
  minLength: 3,
  async: true,
  source: function (query, processSync, processAsync) {
    processSync(['This suggestion appears immediately', 'This one too']);
    return $.ajax({
      url: "/hotbed/sggl/getpipedaddresses", 
      type: 'POST',
      data: {searchItem: query},
      dataType: 'json',
      success: function (json) {
        // in this example, json is simply an array of strings
        return processAsync(json);
      }
    });
  }
});

由于此问题有公开悬赏,我无法将其标记为重复,但您可能会在以下问题中找到更多详细信息,

Duplicate of this question

关于javascript - typeahead js不显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48501988/

相关文章:

javascript - 禁止将 Google Analytics cookie 发送到无 cookie 域

javascript - Node.js 在屏幕上绘图

twitter-bootstrap-3 - Typeahead js 破坏了 bootstrap 内联形式

typeahead.js - 将预先输入的建议包装在表格中

javascript - 如何在React Material-UI组件中调用方法

javascript - jQuery 检查可见性无法正常工作

javascript - 如何让函数变得简单

javascript - typeahead.js : How to remove value when value not in suggestion

javascript - 使用 Typeahead.js 突出显示多个单词

JavaScript Typeahead 自动完成匹配重音和波浪号问题