css
.autocomplete-w1 { background:url(../img/indicator.gif) no-repeat top left; position:absolute; top:0px; left:0px; margin:8px 0 0 6px; /* IE6 fix: */ _background:none; _margin:0; }
.autocomplete { border:1px solid #999; background:#FFF; cursor:default; text-align:left; max-height:350px; overflow:auto; margin:-6px 6px 6px -6px; /* IE6 specific: */ _height:350px; _margin:0; _overflow-x:hidden; }
.autocomplete .selected { background:#F0F0F0; }
.autocomplete div { padding:2px 5px; white-space:nowrap; }
.autocomplete strong { font-weight:normal; color:#3399FF; }
JS
<script src="/static/js/jquery.autocomplete-min.js"></script>
<script>
$(function(){
var onAutocompleteSelect =function(value, data) {
//根据返回结果自定义一些操作
};
$('#compDpt').autocomplete({
serviceUrl: '/index.php?do=ajax&view=setting&type=1',
width: 166,//提示框的宽度
delimiter: /(,|;)\s*/,//分隔符
//onSelect: onAutocompleteSelect,//选中之后的回调函数
deferRequestBy: 0, //单位微秒
//params: { country: 'Yes' },//参数
noCache: false, //是否启用缓存 默认是开启缓存的
dataType: 'json',//数据类型
onSelect: function (value, data) {
$("#company_department").val(data);
}
});
});
</script>
php
$suggestions = $data = array();
foreach ($res as $row){
$suggestions[] = $row['name'];
$data[] = $row['id'];
}
$return = array('query'=>$query, 'suggestions'=>$suggestions, 'data'=>$data);
echo json_encode($return);
exit;
参考文章:http://www.cnblogs.com/maxiao/archive/2012/04/25/2470712.html