时间:2021-07-01 10:21:17 帮助过:21人阅读
输出格式Sparta|896
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script type="text/javascript" src="include/javascript/jquery.js"></script>
<script type="text/javascript" src="include/javascript/jquery.autocomplete.js"></script>
<link rel="stylesheet" type="text/css" href="include/javascript/jquery.autocomplete.css" />
<title>自动完成测试</title>
</head>
<body>
<input type="text" name="keyword" id="search" size="30" />
<script language="javascript">
$(document).ready(function() {
$("#search").autocomplete(
"getindex.php",
{
delay:10,
minChars:1,
matchSubset:1,
matchContains:1,
cacheLength:10,
onItemSelect:selectItem,
onFindValue:findValue,
formatItem:formatItem,
autoFill:false
}
);
});
function findValue(li) {
if( li == null ) return alert("No match!");
if( !!li.extra ) var sValue = li.extra[0];
else var sValue = li.selectValue;
}
function selectItem(li) { findValue(li);}
function formatItem(row) { return row[0];//return row[0] + " (id: " + row[1] + ")"//如果有其他参数调用row[1],对应
<?php
header("ContentType:text/plain;charset:gb2312");
define('SCRIPTNAV', 'getindex');
require_once './include/common.inc.php';
$keyWord=iconv('utf-8', 'gb2312', js_unescape($q));
$query = $db->query("SELECT DISTINCT(shopname) FROM {$dbpre}shops WHERE shopname LIKE '%$keyWord%' GROUP BY shopname ORDER BY addtime DESC LIMIT 0,10");
if($query)
{
while ($result = $db->fetch_array($query))
{
echo $result['shopname']."\n";
}
}
//转换js escape提交过来数据
function js_unescape($str)
{
$ret = '';
$len = strlen($str);
for ($i = 0; $i < $len; $i++)
{
if ($str[$i] == '%' && $str[$i+1] == 'u')
{
$val = hexdec(substr($str, $i+2, 4));
if ($val < 0x7f) $ret .= chr($val);
else if($val < 0x800) $ret .= chr(0xc0|($val>>6)).chr(0x80|($val&0x3f));
else $ret .= chr(0xe0|($val>>12)).chr(0x80|(($val>>6)&0x3f)).chr(0x80|($val&0x3f));
$i += 5;
}
else if ($str[$i] == '%')
{
$ret .= urldecode(substr($str, $i, 3));
$i += 2;
}
else $ret .= $str[$i];
}
return $ret;
}
?>
附件如下:
http://xiazai.jb51.net/200912/yuanma/jquery_autocomplete_php.rar