2014-01-22 171 views
1

我有這個js腳本:jQuery的自動完成功能不顯示結果框

$('#other_teacher').autocomplete({ 
    source: function (request, response) { 
     $.ajax({ 
      url: '/app_dev.php/en/teacher/ajax/courseadd-teacher/'+request.term, 
      type: 'GET', 
      dataatType: 'json', 
      success: function(data) { 
       console.log(data); 
       response($.each(data, function(index, value) { 
        return { 
         label: value, 
         value: index 
        } 
       })); 
      } 
     }); 
    }, 
    minLength: 1 
}); 

和Ajax請求的結果是:

[{"id":2,"name":"Moran bob"},{"id":2,"name":"Willam Lawsan"}] 

阿賈克斯resquest開始,數據有結果的對象,但結果窗口不顯示。

+0

你能張貼您的HTML也請 – dcodesmith

+0

把這個CSS http://code.jquery.com/ui/ 1.10.3/themes/smoothness/jquery-ui.css並嘗試使用我們的代碼或我的 – Dinesh

+0

有什麼好運?這是一個很好的做法,讓我們知道你的進步 – dcodesmith

回答

0

爲自動完成放CSS和那就試試這個代碼

$('#textbox id').autocomplete({ 
     source: function (request, response) { 
      $.getJSON("url?term=" + request.term, function (data) { 
       response(data); 

      }); 

     }, 
     minLength: 1, 
     delay: 100 

    }); 
1

使用$.map而不是$.each,然後訪問從對象的字段的值。 我還增加了一個選擇功能和記錄器內進行確認檢查,如果你實際上得到任何東西

$('#other_teacher').autocomplete({ 
    source: function (request, response) { 
     $.ajax({ 
      url: '/app_dev.php/en/teacher/ajax/courseadd-teacher/'+request.term, 
      type: 'GET', 
      dataatType: 'json', 
      success: function(data) { 
       console.log(data); 
       response($.map(data, function(item) { 
        return { 
         label: item.id, // or item.name if you want 
         value: item.name 
        } 
       })); 
      } 
     }); 
    }, 
    minLength: 1, 
    select: function (event, ui) { 
     console.log(ui.item); 
    } 
});