2012-07-31 32 views
0

對於你們中的一些人來說,這可能非常容易,但從第一次開始就非常困難。jquery自動建議示例不起作用

通過查看網絡上的一些示例,我最終得到了以下代碼用於自動建議示例,但代碼無效。

感謝

HTML

<html> 
<head> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() 
     { 
      $('#textbox_postcode').autocomplete(
      { 
       source: 'search-db.php', 
       minLength: 3 
      }); 
     }); 
    </script> 
</head> 

<body> 
    <form action="search.php" method="post"> 
     <input type="text" id="textbox_postcode" value="" />&nbsp;&nbsp;&nbsp;<input type="submit" value="Search" /> 
    </form> 
</body> 
</html> 

PHP

$keyword = ltrim(strtolower(strip_tags($_GET['keyword'])));

if (! $keyword) return;

$host = 'localhost'; $user = 'root'; $pswd = ''; $dtbs = 'geomaps';

$host_conn = mysql_connect($host, $user, $pswd); $dtbs_conn = mysql_select_db($dtbs);

$return = array();

$sql = "SELECT id, postcode FROM postcodes WHERE postcode LIKE '$keyword%' ORDER BY postcode"; $run = mysql_query($sql);

if (@mysql_num_rows($run) == 0) return;

while ($records = mysql_fetch_array($run, MYSQL_ASSOC)) { $return[] = $records; }

echo json_encode($return);

+0

,什麼是你的錯誤。 – Doa 2012-07-31 15:29:32

+0

當我第一次加載頁面時,我得到這個錯誤:'未捕獲的類型錯誤:對象[對象對象]沒有方法'自動完成'。當我輸入內容時,什麼都沒有發生。 – BentCoder 2012-07-31 15:32:44

+2

在jQuery UI中不自動完成? – 2012-07-31 15:33:53

回答

1

嘗試改變$_GET['keyword']$_GET['term']

+0

天哪,爲什麼期限工作。它保留工作還是什麼? – BentCoder 2012-07-31 16:24:11

+0

我檢查了docs,並將「term」追加到了URL而不是「keyword」。 http://jqueryui.com/demos/autocomplete/向下滾動到底部,你會看到它使用'term'。 – Zac 2012-07-31 16:25:37

+0

你救了我的命。謝謝 – BentCoder 2012-07-31 16:28:33