我正在尝试使用带有 ajax 调用的 JQuery 自动完成功能,它将返回字符串列表。当我尝试键入时,它显示所有字符串列表,而不是根据输入过滤掉..不知道我哪里出错了..下面是我的自动完成功能..
$("#domainNameId").autocomplete({
source : function(request, response) {
console.log("in ajax ");
$.ajax({
url : "getAllDomains",
type : "GET",
contentType : "application/json",
data : {
env : $("#environment").val()
},
dataType : "json",
success : function(data) {
response(data); // list of strings..
},
error : function(x, t, m) {
console.trace();
if (!(console == 'undefined')) {
console.log("ERROR: " + x + t + m);
}
console.log(" At the end");
}
});
},
});
感谢帮助..
最佳答案
您的后端似乎总是返回全部数据而不进行任何过滤(服务名称本身是 getAllDomains
)。在这种情况下,不需要使用 source 选项的函数形式来进行 ajax 调用。
您正在做的是在用户输入时向服务器发送多个 AJAX 请求。如果你的后端总是返回相同的数据,那么向它发送多个请求是没有意义的。您可以简单地获取数据一次,然后使用响应作为源初始化自动完成,然后小部件将根据用户类型进行过滤。
文档说:
A response callback, which expects a single argument: the data to suggest to the user. This data should be filtered based on the provided term.
因此,如果您的服务器不进行过滤,请不要使用函数形式来发出 AJAX 请求。
做类似的事情:
$(function() {
// make a one-time request
$.ajax({
url: "getAllDomains",
type: "GET",
contentType: "application/json",
dataType: "json",
success: function(data) {
// init the widget with response data and let it do the filtering
$("#domainNameId").autocomplete({
source: data
});
},
error: function(x, t, m) {
console.trace();
if (!(console == 'undefined')) {
console.log("ERROR: " + x + t + m);
}
console.log(" At the end");
}
});
});
关于javascript - jquery 自动完成 - 不过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37799471/