jquery 插件自动完成 - 不显示结果
我在使用自动完成插件时遇到问题。这是我的代码:
var autocomplete = {
cache: {},
xhr: {}
};
$('#header .search input[type="text"]').autocomplete({
minLength : 2,
source: function (request,response) {
var q = request.term;
if( q in autocomplete.cache ) {
response( autocomplete.cache[q] );
return false;
} else {
autocomplete.hxr = $.getJSON("/search/autocomplete/nous?ajax=1&q="+q,function(data, status, xhr) {
autocomplete.cache[q] = data;
//if( autocomplete.xhr === xhr ) {
response(data);
//}
});
return true;
}
}
});
当我在输入中写入某些内容(在本例中为“Hello”)时,我可以在 Web 开发人员工具中看到它返回一个 json 数组。因此,请求完成后我会收到有效的响应。
0: "hello kitty"
1: "hello dolly and frieda"
2: "hello ass"
3: "hello there"
4: "hello do you make"
它正在执行 ajax 请求,但结果没有被推送到下拉菜单中,它是空的。任何帮助表示赞赏!
谢谢!
I'm having a problem with the Auto complete plug-in. Here is my code:
var autocomplete = {
cache: {},
xhr: {}
};
$('#header .search input[type="text"]').autocomplete({
minLength : 2,
source: function (request,response) {
var q = request.term;
if( q in autocomplete.cache ) {
response( autocomplete.cache[q] );
return false;
} else {
autocomplete.hxr = $.getJSON("/search/autocomplete/nous?ajax=1&q="+q,function(data, status, xhr) {
autocomplete.cache[q] = data;
//if( autocomplete.xhr === xhr ) {
response(data);
//}
});
return true;
}
}
});
When I'm writing something in the input ("Hello" in this case), I can see in the web developer tool that its returning a json array. So, I'm getting a valid response when the request is done.
0: "hello kitty"
1: "hello dolly and frieda"
2: "hello ass"
3: "hello there"
4: "hello do you make"
It is doing the ajax requests but the results are not being pushed into the drop-down menu, it's empty. Any help is appreciated!!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个答案基于评论:
这是使用延迟对象实现相同目标的方法:
编辑:看起来原始代码无法工作的原因是由于拼写错误。
Edit2:添加失败处理程序
This answer is based on comments:
This is how you could accomplish the same goal using deferred objects:
Edit: looks like the reason the original code wouldn't work was due to the typo.
Edit2: added fail handler