0

我试图在执行 ajax 请求后返回 bootstrap typeahead 的结果,但它不起作用但是当我不启动 ajax 请求时它确实起作用。

这不起作用:

$(".typeahead").typeahead({
  source: function(query, process) {
    return $.ajax({
      url: "/typeahead",
      type: "GET",
      data: "action=" + query,
      success: function(result) {
        return result;  // this returns an array checked with console
      }
    });
  }
});

没有ajax它可以工作:

  $(".typeahead").typeahead({
      source: function(query, process) {
          return ["option1", "option2", "option3"]
      }
    });
4

2 回答 2

2

您应该返回 process(result) 而不是只返回结果。

$(".typeahead").typeahead({
  source: function(query, process) {
    return $.ajax({
      url: "/typeahead",
      type: "GET",
      data: "action=" + query,
      success: function(result) {
        return process(result);  // this returns an array checked with console
      }
    });
  }
});
于 2013-03-28T13:02:27.813 回答
0

在一个函数中设置ajax调用..

function typeahead()
    {
    $.ajax({
          url: "/typeahead",
          type: "GET",
          data: "action=" + query,
          success: function(result) {
            return result;  // this returns an array checked with console
          }
        });
    }

在initilze之后你把这样的代码

$(".typeahead").typeahead({
      source: function(query, process) {
         var data=typeahead(query);
         process(data);

      }
    });
于 2013-03-28T12:58:04.077 回答