自动补全,如果把matchContains :true,当data包括我输入的数据,文本框就会提示,如果matchContains :false,因为我自动补全是通过三个字段(汉字、全拼、简拼)一起的,所以这两种都不符合我的要求。matchContains : 'word',这种对英文支持的很好,但是中文就查询不出来了,所以接下来就修改jquery.autocomplete.js里的代码,
源代码
function matchSubset(s, sub) {
if (!options.matchCase)
s = s.toLowerCase();
var i = s.indexOf(sub);
if (options.matchContains == "word"){
if(/.*[\u4e00-\u9fa5]+.*$/.test(sub)){//如果是中文
//因为我自动补全是通过三个字段匹配的,中间用空格连在一起的,所以(s.split(' '))[1]得到中文那个字段然后再去匹配,得到我想要的
i = ((s.split(' '))[1]).substring(0,sub.length).search(sub);
}
else //如果是英文不做修改
i = s.toLowerCase().search("\\b" + sub.toLowerCase());
}
if (i == -1) return false;
return i == 0 || options.matchContains;
};
下面是我js的代码
$('#searchEquipTxt').autocomplete(data, {
max : 10, // 列表里的条目数minChars : 0, // 自动完成激活之前填入的最小字符
dataType : "json",
width : 400, // 提示的宽度,溢出隐藏
scrollHeight : 300, // 提示的高度,溢出显示滚动条
matchContains : 'word', // 包含匹配,就是data参数里的数据,是否只要包含文本框里的数据就显示
autoFill : false,
formatItem : function(row, i, max) {
return row.HZ + '[' + row.QP + ']';
},
formatMatch : function(row, i, max) {
return row.QP + " " + row.HZ + " " + row.JP;
},
formatResult : function(row) {
return row.HZ;
}
}).result(function(event, row, formatted) {
});