php源代碼:
function getAllYishiAjax($leibie) {
$countys=$this->MYiShi->queryCountrow($leibie);
$datays=$this->MYiShi->findAllYS($leibie);
$json='[';
for ($i=0;$i<$countys;$i++){
$json.='{name:';
$json.=json_encode($datays[$i][m_yishi][name]).',id:'.json_encode($datays[$i][m_yishi][id]).'},';
}
$json=substr($json,0,strlen($json)-1);
$json.=']';
echo $json;
exit;
//$this->set("json",$json);
}
js代碼:
$(document).ready(
function() {$.ajax({
type : "POST",
contentType : "application/json",
url : "/top/getAllYishiAjax/1",
data : "{}",
dataType : "json",
error : function(msg) {
//alert(unescape(msg.responseText));//解碼,顯示漢字
var datas = eval('(' + msg.responseText + ')');
$('#zhuzhenyishi_shuru').autocomplete(
datas,
{
max : 5, // 列表裏的條目數
minChars : 0, // 自動完成激活之前填入的最小字符
width : 70, // 提示的寬度,溢出隱藏
scrollHeight : 230, // 提示的高度,溢出顯示滾動條
matchContains : true, // 包含匹配,就是data參數裏的數據,是否只要包含文本框裏的數據就顯示
autoFill : false, // 自動填充
scroll : false, // 當結果集大於默認高度時是否使用卷軸顯示
formatItem : function(row, i, max) {
return row.name;
},formatMatch : function(row, i, max) {
return row.name;
},formatResult : function(row) {
return row.name;
}
}).result(function(event, row, formatted) {
$('#zhuzhenyishi').val(row.id);
});
$('#fuzhuyishi_shuru').autocomplete(
datas,
{
max : 5, // 列表裏的條目數
minChars : 0, // 自動完成激活之前填入的最小字符
width : 70, // 提示的寬度,溢出隱藏
scrollHeight : 230, // 提示的高度,溢出顯示滾動條
matchContains : true, // 包含匹配,就是data參數裏的數據,是否只要包含文本框裏的數據就顯示
autoFill : false, // 自動填充
scroll : false, // 當結果集大於默認高度時是否使用卷軸顯示
formatItem : function(row, i, max) {
return row.name;
},formatMatch : function(row, i, max) {
return row.name;
},formatResult : function(row) {
return row.name;
}
}).result(function(event, row, formatted) {
$('#fuzhuyishi').val(row.id);
});
}
});
});