2011-11-29 144 views
0

我遇到了Auto complete插件的問題。這裏是我的代碼: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; 
     } 
     } 
    }); 

當我在輸入的書寫的東西(「你好」,在這種情況下),我可以在web開發工具,它返回一個JSON數組看到。所以,當請求完成時,我得到了有效的響應。

0: "hello kitty" 
1: "hello dolly and frieda" 
2: "hello ass" 
3: "hello there" 
4: "hello do you make" 

它正在做ajax請求,但結果沒有被推入下拉菜單,它是空的。任何幫助表示讚賞!

謝謝!

+0

這是錯字? 'autocomplete.hxr' –

+0

在源函數中返回true或false的目的是什麼?它似乎沒有記錄。 –

+0

我真的試圖調試別人的代碼,而這個人不再在這裏......但它是爲了避免多餘的請求而實現緩存。 – Lelly

回答

1

這個答案是基於評論:

這是你如何能使用延遲對象完成相同的目標:

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); 
     });  
    } 
}); 

編輯:貌似理由的原代碼是行不通的是由於錯字。

編輯2:添加失敗處理程序

+0

好的。似乎錯字沒有解決問題,即時獲得相同的結果;一個空的下拉菜單。我也試過你的代碼,結果相同。任何其他想法? – Lelly

+0

嘗試修改我的代碼,發出警報嗎? –

+0

嗯...請記住我的代碼需要更新版本的jQuery,它不會在jQuery 1.4.4或更高版本中工作。 –