jquery autocomplete 中文乱码解决办法
jquery.autocomplete.js
$.ajax({
// try to leverage ajaxQueue plugin to abort previous requests
mode: "abort",
// limit abortion to this input
port: "autocomplete" + input.name,
dataType: options.dataType,
url: options.url,
data: $.extend({
q: encodeURI(lastWord(term)),
limit: options.max
}, extraParams),
success: function(data) {
var parsed = options.parse && options.parse(data) || parse(data);
cache.add(term, parsed);
success(term, parsed);
}
});
c# 后台接收代码,我用的是ASHX文件
string q=System.Web.HttpUtility.UrlDecode(context.Request["q"].ToString(), UTF8Encoding.UTF8);
前台实例
$(function() {
$("#ctl00_ContentPlaceHolder2_TextBoxen").autocomplete("../getemployee.ashx", {
delay: 5,
matchCase: false,
selectFirst: true,
cacheLength: 3,
matchSubset:false,
minChars: 0, //最少输入字条
max: 12,
autoFill: false, //是否选多个,用","分开
mustMatch: false, //是否全匹配, 如数据中没有此数据,将无法输入
matchContains: true, //是否全文搜索,否则只是前面作为标准
scrollHeight: 220,
width: 500,
multiple: false,
parse: function(data) { //重写格式化得到的json集合因为原有方法无法解析通过webservice回调的值
var rows = new Array();
var datas = data;
for (var i = 0; i < datas.length; i++) {
//alert(datas[i])
rows[i] = { data: datas[i], employeename: datas[i].employeename, unitname: datas[i].department };
}
return rows;
},
formatItem: function(row, i, max) { //显示格式
return "<span style='width:140px'>" + row.employeename + "</span> <span style='width:120px'>" + row.department + "</span>";
},
formatMatch: function(row, i, max) { //以什么数据作为搜索关键词,可包括中文,
return row.employeename;
},
formatResult: function(row) { //返回结果
return row.employeename;
}
});
$("#ctl00_ContentPlaceHolder2_TextBoxen").result(function(event, row, formatted) { //额外触发,可不要
$("#ctl00_ContentPlaceHolder2_TextBoxen").val(row.employeename);
$("#ctl00_ContentPlaceHolder2_TextBoxeid").val(row.employeeid);
$("#ctl00_ContentPlaceHolder2_TextBox1").val(row.cardnum);
$("#ctl00_ContentPlaceHolder2_TextBoxdepart").val(row.department);
$("#ctl00_ContentPlaceHolder2_TextBoxL").val(row.carnum);
$("#ctl00_ContentPlaceHolder2_TextBoxDriver").val(row.cardriver);
$("#ctl00_ContentPlaceHolder2_TextBoxunit").val(row.unit.split('/')[2]);
if (row.carnum.length != 0 || row.cardriver.length != 0) {
$("#ctl00_ContentPlaceHolder2_CheckBoxCar").attr("checked", "checked");
}
$("#ctl00_ContentPlaceHolder2_CheckBoxPeople").attr("checked", "checked");
//更多操作
});
})