我尝试从http://www.devbridge.com/projects/autocomplete/jquery/实现这个自动建议
我的 PHP 代码不工作,但查找功能只显示结果。请指导我这件事。
这是我的代码:
$(document).ready(function()
{
$('#searchresult').autocomplete(
{
serviceUrl: 'source.php',
minChars: 1,
delimiter: /(,|;)\s*/, // regex or character
maxHeight: 400,
zIndex: 9999,
deferRequestBy: 0, //miliseconds
noCache: false, //default is false, set to true to disable caching
// callback function:
onSelect: function(value, data){ alert('You selected: ' + value + ', ' + data); },
// local autosugest options:
lookup: ['January', 'February', 'March', 'April', 'May'] //local lookup values
});
});
PHP 代码:source.php
<?php
include "config/config.php";
require "jsonwrapper/jsonwrapper.php";
//$term = trim(strip_tags($_GET['term']));//retrieve the search term that autocomplete sends
//$qstring = "SELECT description as value,id FROM tblcompanies WHERE description LIKE '%".$term."%'";
$result = mysql_query("SELECT fieldDesc AS value, fieldID AS id FROM table");
$data = array();
while ($row = mysql_fetch_array($result))//loop through the retrieved values
{
$row['value'] = htmlentities(stripslashes($row['value']));
$row['id'] = (int)$row['id'];
$data[] = $row;//build an array
}
echo json_encode($data);//format the array into json data
?>
如果您实现这种类型的 jquery 自动建议,请分享。我很高兴有任何建议。