2011-11-21 93 views
0

大家好我已經看到所有與此問題有關的問題,但無法找到解決方案。我正在與jquery ui自動完成。我的情況是這樣的,jquery ui自動完成不能正確工作

我有一個複選框。當它被選中時,一個ajax請求被髮送到服務器,該服務器從數據庫獲取一些信息並返回json編碼數據。在成功函數中,我調用jquery自動完成,並在源代碼中提供我的ajax請求返回的數據。當我開始在我的文本框中寫一些東西,(我可以在Firebug的控制檯選項卡中看到)一個get請求發送有我所有的選項,這是從Ajax請求返回這樣

http://localhost/project/module/controller/index/%5B%22Mobile%22,%22Watch%22,%22Lamps%22,%22Harry%20Potter%22,%22Suitcase%22,%22Halloween%22,%22Xmas%22,%22Multiple%22,%22oil%22,%22Empty%20Keywords%22,%22iphone%20theme%22,%22Edit%20theme%22,%22Final%20Theme%22%5D?term=mo 

並沒有任何反應。沒有選項顯示爲自動完成。我的代碼如下

<input type="checkbox" name="prebuilt-Themes" id="prebuilt-Themes" onclick="get_all_themes();"> 

文本框

<input type="text" id="themes" style="display: none;" class="ui-autocomplete-input"> 

和處理Ajax請求的功能

function get_all_themes() 
{ 

    if($("#prebuilt-Themes").is(':checked')) 
    {  
    $('#themes').show(); 
    $.ajax({ 
      type:'POST', 
      //data:({string:'abc'}), 
      url: "<?= $this->baseUrl(); ?>/module/controller/getallthemes", 
      success:function(data) 
      { 
       $(function() { 
       $("#themes").autocomplete({ 
        source: data 

         }); 
       }); 
      } 
     }); 
    } 


} 

這是我從我的Ajax請求接收的數據看起來像這樣

["Mobile","Watch","Lamps","Harry Potter","Suitcase","Halloween","Xmas","Multiple","oil","Empty Keywords","iphone theme","Edit theme","Final Theme"] 

Can an an大家指導我我做錯了什麼?

回答

1

您需要在與ID,標籤JSON格式Ajax響應,和值,像這樣:

[ { "id": "Luscinia svecica", "label": "Bluethroat", "value": "Bluethroat" }, { "id": "Motacilla flava", "label": "Blue-headed Wagtail", "value": "Blue-headed Wagtail" }, { "id": "Cyanistes caeruleus", "label": "Blue Tit", "value": "Blue Tit" }, { "id": "Monticola solitarius", "label": "Blue Rock Thrush", "value": "Blue Rock Thrush" }, { "id": "Anas discors", "label": "Blue-winged Teal", "value": "Blue-winged Teal" }, { "id": "Merops persicus", "label": "Blue-cheeked Bee-eater", "value": "Blue-cheeked Bee-eater" }, { "id": "Tarsiger cyanurus", "label": "Red-flanked Bluetail", "value": "Red-flanked Bluetail" } ]