我尝试从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 autosuggest,请分享。我很高兴有任何建议。
答案 0 :(得分:1)
如果要使用服务器的结果,请删除“查找”选项。
答案 1 :(得分:0)
您的代码看起来很好,但我看到的唯一问题是查找。
您可以将JSON编码数组传递给js进行查找吗?
这样的事情:
$(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: resultArray
});
});
结果数组这里是JSON Encode php数组传递给js进行查找。
希望在完成此操作后,它将被修复。