jquery autocomplete用法与异常

下载jquery autocomplete
[b]
<script type="text/javascript" src="${webRoot}/js/jquery-1.8.0.min.js"></script>
<link rel="stylesheet" type="text/css" href="${webRoot}/css/jquery.autocomplete.css" />
<script type='text/javascript' src='${webRoot}/js/jquery.autocomplete.js'></script>
[/b]
[b]
页面使用
[/b]


$("#nameinfo").focus().autocomplete("${webRoot}/XXX.do", {
minChars:1,
width: 310,
autoFill: false,
dataType:'json',
mustMatchPro:'id',//填写缓存想要匹配的属性名称,这个是自定义的
extraParams:{sizeOfPage:50},//额外附加参数
parse:function(data){//返回结果集解析

var list=data.objects;
var arrayHtml = [];
if(null!=list&&list.length>0){
for(var i=0;i<list.length;i++){
var arrvalue={
id:list[i].id,
name:list[i].name,
showItem:list[i].showItem
};
arrayHtml.push(arrvalue);
}
}
//解析结果集
return $.map(arrayHtml, function(row) {
return {
data : row,//返回的行对象
value : row,
result :row.id//选中的结果
}
});
},//解析数据
formatItem: function(row, i, max) {

return row.showItem;
},
formatResult: function(item) {
return item.id;
}
}).result(function(e, item) {
//获取返回值
//alert(item.id);
});


[b]
当使用matchSubset:true时出现异常 s.toLowerCase();
[/b]

出问题的地方


function matchSubset(s, sub) {

if (!options.matchCase) s = s.toLowerCase();
//当前获得匹配项是json数组而不是字符串所以不支持这个方法


解决办法,修改jquery.autocomplete.js,添加一个属性mustMatchPro




$.Autocompleter.defaults = {
inputClass: "ac_input",
resultsClass: "ac_results",
loadingClass: "ac_loading",
minChars: 1,
delay: 400,
matchCase: false,
matchSubset: true,
[b] mustMatchPro:'id',[/b]
matchContains: false,
cacheLength: 10,
max: 100,
mustMatch: false,
extraParams: {},
selectFirst: true,
formatItem: function(row) { return row[0]; },
formatMatch: null,
autoFill: false,
width: 0,
multiple: false,
multipleSeparator: ", ",
highlight: function(value, term) {
return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>");
},
scroll: true,
scrollHeight: 180
};


function matchSubset(s, sub) {

if (!options.matchCase) {
try{
s = s.toLowerCase();
}catch(e){
s=s[options.mustMatchPro].toLowerCase();
}
}

var i = s.indexOf(sub);
if (options.matchContains == "word"){
i = s.toLowerCase().search("\\b" + sub.toLowerCase());
}
if (i == -1) return false;
return i == 0 || options.matchContains;
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值