2013-03-08 105 views
0

我使用jQuery的自動完成UI和我在JSON格式類似這樣的如何在JQuery自動完成UI中顯示Json數據?

[{"organization_name":"health info"},{"organization_name":"Canada health"},{"organization_name": "org 1"}] 

及彼數據是不顯示JSON數據我jQuery代碼正確

$('input[name=profileOrg]').autocomplete({ 
        source:'CHI_custom/customScripts/getorgname.php', 
        dataType: 'json', 
        minLength:2 

       }); 

誰能幫助哪有顯示數據在自動完成textboX?

+4

有你閱讀文檔 – Daisy 2013-03-08 18:50:17

+0

你需要'標籤:STR,值:str'對在你的JSON中。 – Ohgodwhy 2013-03-08 18:51:47

+0

@Ohgodwhy你的意思是這樣的'[{lable:organization_name,value:health info},{lable:organization_name,value:value2}]' – user1878049 2013-03-08 19:00:09

回答

0

您需要更改自動完成的方式顯示項目jQuery的文檔上

$('input[name=profileOrg]').autocomplete({ 
    source:'CHI_custom/customScripts/getorgname.php', 
    dataType: 'json', 
    minLength:2, 
    select: function (event, ui) { 
     $(this).val(ui.item.organization_name); 
     return false; 
    } 
}) 
.data("autocomplete")._renderItem = function (ul, item) { 
     return $("<li></li>") 
      .data("item.autocomplete", item) 
      .append('<a>' + item.organization_name + '</a>') 
      .appendTo(ul); 
}; 

更多信息:http://jqueryui.com/autocomplete/#custom-data

+0

謝謝你的代碼對我來說非常感謝你...你是我的救世主 – user1878049 2013-03-08 19:13:28

+0

你的代碼工作正常,但是當我從列表中選擇名字的時候,名字不會顯示在文本框中 – user1878049 2013-03-08 19:22:49

+0

你是對。您需要將'select'選項添加到該函數。回答編輯。 – 2013-03-08 19:28:31