2012-05-08 61 views
2

我想在jQuery自動完成中顯示json數據,並且除了下拉菜單不顯示,一切正常。
我可以使用parse:(其中有很多例子),但我想我需要的數據通過source:顯示。jquery自動完成數據格式

是否可以顯示數據的方式,我正在做或我們需要解析?
如果是的話,怎麼樣?

我不是很使用jQuery UI

$(function() { 
    function log(message) { 
     $("<div/>").text(message).prependTo("#log"); 
     $("#log").attr("scrollTop", 0); 
    } 

    $("#aut_teachers").autocomplete({ 
     source: function(request, response) { 
      $.ajax({ 
       url: base_url+"controller/model", 
       data: request, 
       dataType: "json", 
       type: "post", 
       success: function(data){ 
        response(data.value);// here is where the problem is 
       } 

      }); 
     }, 
     minLength: 2, 
     select: function(event, ui) { 
      log(ui.item ? 
      "Selected: " + ui.item.value + " aka " + ui.item.id : 
      "Nothing selected, input was " + this.value); 
     } 
    }); 
}); 
+0

在jQuery中,數據已經是您的JSON對象。 我認爲你不需要使用data.value。 你能舉一個你的JSON數據的例子嗎? – LawfulHacker

+0

yes,here: {「response」:「true」,「message」:[{「value」:「Wren J Thomas」,「label」:「1262」},{「value」:「Wiseman Kim」, 「label」:「1257」},{「value」:「Whitman」,「label」:「1249」},{「value」:「Whitehead Marcia」,「label」:「1248」},{「value」 :「White Maja」,「label」:「1247」}]} –

回答

2

好你應該使用data.message作爲參數做response代替data.value

因爲data.message是一個對象數組,而對象是標籤/值,所以可以使用它。

檢查Autocomplete Docs瞭解更多信息。