2

不管我在文本框中输入什么,总是显示整个列表。这是我的代码:

$("#tb_country").autocomplete({
  source: function(request, response) {
    $.ajax({
      url: "/index.php/ajax_calls/select_countries",
      dataType: "json",
      data:  request,
      success: function(data) {
        response(data);
      }
    });
  },
  minLength: 3
});

select_countries函数以正确的自动完成格式返回 JSON 数据,即[{"label":"United States"},{"label":"Canada"}........] 请有人告诉我出了什么问题有了这个,谢谢

4

2 回答 2

5

确保您实际上在服务器端代码中过滤了您的响应,如下所述:

自动完成插件不过滤结果,而是添加了一个带有术语字段的查询字符串,服务器端脚本应该使用它来过滤结果。例如,如果源选项设置为“http://example.com”并且用户键入 foo,则会向http://example.com?term=foo发出 GET 请求。

您确实发送了正确的请求(term参数由input元素的当前值填充),但很可能不在服务器代码中使用它。

于 2012-10-26T13:28:08.170 回答
0

也许问题是响应不在上下文中?

除了使用 response(data) 之外,您还可以尝试从服务器获取数据的 onchange,而不仅仅是像这样设置源:

$("#tb_country").autocomplete( "option", "source", serverData );

Also I didn't understood if the problem is in the returned data from the server or in the jquery. If the problem is the data from the server you should make sure what is being sent in the request.

Edit: Must be a server problem since data was already filled once. Can you please please post the Request Headers and or server function?

于 2012-10-26T13:31:16.783 回答