如果你想使用基本的JQuery的,你可以在例如在remoteDatasource例子看看 http://jqueryui.com/autocomplete/#remote
你可以看到:
$("#birds").autocomplete({
source: "search.php",
minLength: 2,
select: function(event, ui) {
log(ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value);
}
});
minlenght是最小的信,你可以輸入開始研究。
在你不得不創造一些像
//connect to your database
$term = trim(strip_tags($_GET['term']));//retrieve the search term that autocomplete sends only when there are 2 char or more
$qstring = "SELECT description as value,id FROM test WHERE description LIKE '%".$term."%'"; //ONLY AN EXAMPLE
$result = mysql_query($qstring);//query the database for entries containing the term
while ($row = mysql_fetch_array($result,MYSQL_ASSOC))//loop through the retrieved values
{
$row['value']=htmlentities(stripslashes($row['value']));
$row['id']=(int)$row['id'];
$row_set[] = $row;//build an array
}
echo json_encode($row_set);//format the array into json data
您需要uotput這樣的JSON東西,如果你不是跨域服務器端(跨域你需要JSONP但這是另一回事)
[{"value":"Some Name","id":1},{"value":"Some Othername","id":2}]
最差作法 另一種解決方案(但不是邏輯上確定)是創建整個JS陣列上記錄一個循環,產生一字符串。
var availableTags = [「ActionScript」,「AppleScript」,「Asp」,...];
所以你有你的表硬編碼和不可變的頁面,但每個數據都暴露在頁面的源代碼中,它只是爲了學習的目的,但從來沒有在生產中做到這一點。
如果你慣於礦替代模式的表,則需要指定如何,你不會,但自動完成是基於2點的想法。 1)每個關鍵詞有助於做出更好的限制,所以每次按鍵都需要做另一個查詢
2)當新鍵被按下時,最後的結果並不重要,所以最後的請求被放棄。
因此,在某些情況下,當鍵入的鍵很少時,您可以在查詢中使用動態限制,可以將結果限制爲有限的記錄數,當鍵入的序列增長時,您可以刪除限制或限制爲因爲條件使得工作更好,所以記錄數量很大。
我希望對大家有用
謝謝,非常感謝! – user1835249