jquery 插件自动完成 - 不显示结果

发布于 2024-12-18 15:45:07 字数 997 浏览 0 评论 0原文

我在使用自动完成插件时遇到问题。这是我的代码:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦情居士 2024-12-25 15:45:07

这个答案基于评论:

这是使用延迟对象实现相同目标的方法:

var autocomplete = {
    cache: {}
};

$('#header .search input[type="text"]').autocomplete({
    minLength: 2,
    source: function(request, response) {
        var q = request.term;
        if (!autocomplete.cache[q]) {
            autocomplete.cache[q] = $.getJSON("/search/autocomplete/nous?ajax=1&q=" + q);
        }
        autocomplete.cache[q].done(response).fail(function(x,y,z){
            alert(x.responseText + "\n-----\n" + y + "\n" + z);
        });       
    }
});

编辑:看起来原始代码无法工作的原因是由于拼写错误。

Edit2:添加失败处理程序

This answer is based on comments:

This is how you could accomplish the same goal using deferred objects:

var autocomplete = {
    cache: {}
};

$('#header .search input[type="text"]').autocomplete({
    minLength: 2,
    source: function(request, response) {
        var q = request.term;
        if (!autocomplete.cache[q]) {
            autocomplete.cache[q] = $.getJSON("/search/autocomplete/nous?ajax=1&q=" + q);
        }
        autocomplete.cache[q].done(response).fail(function(x,y,z){
            alert(x.responseText + "\n-----\n" + y + "\n" + z);
        });       
    }
});

Edit: looks like the reason the original code wouldn't work was due to the typo.

Edit2: added fail handler

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文