2014-10-03 149 views
0

我試圖使用jQuery UI自動完成但它不顯示任何列表或任何東西。據我所知,沒有必要用jQuery UI聲明ullijQuery UI自動完成不起作用

<html> 
    <head> 
     <link href="./jquery-ui.css" rel="stylesheet"> 

     <script src="./jquery-1.11.1.js"></script> 
     <script src="./jquery-ui.js"></script> 
    </head> 

    <body> 
     <input id="pu_location" size="38" type="text" />        
     <input type="hidden" id="pu_locationID" /> 

     <script> 
      Script is below. 
     </script> 
    </body> 
</html> 

腳本:

<script> 

$(document).ready(function() { 

     $("#pu_location").autocomplete({ 

      source: function(request, response) { 

      $.getJSON("/test.php", { country_code: "USA",term:$('#pu_location').val()}, 
         function(data) { 
         alert(data[0].id); 
         var array =[]; 
         for(key in data){ 
          if (data[key].label!=''){ 
           array.push(data[key].label); 
           } 
          } 
          alert(array); 
          response(array); 
         }); 
      }, 
      delay: 100, 
      minLength: 3       
      }); 
}); 
</script> 

順便說一句,在data是完美的。 alert表明一切都很好。

被修改: 它現在可以與Chrome一起使用,但不能與Firefox一起使用!

+0

你可以格式化代碼位,這將是更多pleasent眼睛:) – Beri 2014-10-03 06:33:58

+0

我試過了。我正在使用一個簡單的文本編輯器。 – Arnold 2014-10-03 06:39:21

+0

@Beri沒有'$(function(){'和'function(request,response){'作爲'$(「#pu_location」)之外的函數。'autocomplete {{代碼看起來好多了,你知道.. 。 – Regent 2014-10-03 06:47:28

回答

0

第一個線索: 刪除:

$(function() { 

你是想在這裏創造selfexecuting JS的功能,但它是沒有必要的,因爲你已經在準備功能annonymus功能。

然後把一個日誌只是線

$("#pu_location").autocomplete({ 

之前只是要確定正在執行該代碼。第二條線索:你在迴應數據時感受數據,然後創建並填充數組,最後傳遞給響應數據,那麼處理的意義是什麼?

+0

它被執行並且警報顯示正確的數據。我剛剛刪除了那個,並且仍然不會顯示檢索到的內容 – Arnold 2014-10-03 06:42:06

+0

也嘗試過使用數據變量和創建的字符串數組,但沒有運氣 – Arnold 2014-10-03 06:43:58

+0

Array理由:我認爲響應無法處理對象,所以我用字符串做了一個數組,沒有工作 – Arnold 2014-10-03 06:46:54