index.html
jQuery Autocomplete Plugin$().ready(function() {
$("#suggest1").autocomplete("ajax.php", {
minChars: 0,
width: 230,
highlight: false,
scrollHeight: 300,
matchContains: "word",
autoFill: false,
parse: function(data) {
return $.map(eval(data), function(row) {
return {
data: row,
value: row.name,
result: row.to
}
}); //对ajax页面传过来的数据进行json转码
},
formatItem: function(row, i, max) {
return i + '/' + max + ':"' + row.name + '"[' + row.to + ']';
},
formatMatch: function(row, i, max) {
return row.name + row.to;
},
formatResult: function(row) {
return row.to;
}
}).result(function(event, row, formatted) {
return row.to;
});
});
jQuery Autocomplete Plugin local deomo
选择客户:
index结束
ajax.php
$conn = @mysql_connect("localhost","root","") or die("Failed!");
mysql_select_db("jquery_test",$conn);
mysql_query("set names 'utf8'");
$q = $_GET["q"];
if (!$q) return;
$items = array();
$sql1 = mysql_query("select `CLIENT_N_SHORT`,`CLIENT_NAME` from `client`");
while($row = mysql_fetch_array($sql1)){
$key = $row['CLIENT_N_SHORT'];
$value = $row['CLIENT_NAME'];
if (strpos($value, $q) !== false) {
array_push($items, array(
"name" => urlencode($key),
"to" => urlencode($value)
));
//防止json中文乱码 因此转码
}
}
//print_r($items);
//exit();
//echo "
";
echo urldecode( json_encode($items) );
?>
有什么不懂的可以追问
原创精品,转载请说明出处http://www.cnblogs.com/jia58960/archive/2012/02/22/2362875.html