使用 jQuery UI 自动完成,如何确保无论输入什么文本都会出现一个特定的结果?
我的自动完成源列表有点像这样:
var items = ['UK', 'IE', 'BE', 'NL', 'PLC'];
我试图确保自动完成的结果集始终包含 特定术语(在本例中为 PLC)。
我尝试过将 PLC 添加到源列表,然后覆盖自动完成结果过滤器(请参阅 此处)。
无论输入什么内容,我都可以让它返回“PLC”:
$("#autocomplete").autocomplete({
source: function(request, response) {
// The term the user searched for;
var term = request.term;
// Extract matching items:
var matches = $.grep(items, function(item, index) {
return /PLC/.test(item);
});
// let autocomplete know the results:
response(matches);
}
});
http://jsfiddle .net/GarethPN/xbZhr/6/
但是我如何在正则表达式中使用术语变量来保留标准功能?
还是有一种我错过的明显更简单的方法?
My auto-complete source list is a little bit like this:
var items = ['UK', 'IE', 'BE', 'NL', 'PLC'];
I am trying to make sure that the result set from my auto-complete always contains a
specific term (in this case PLC).
I have tried is adding PLC to the source list, then overriding the autocomplete result filter (see here).
I can get it to return "PLC" regardless of what is typed in like this:
$("#autocomplete").autocomplete({
source: function(request, response) {
// The term the user searched for;
var term = request.term;
// Extract matching items:
var matches = $.grep(items, function(item, index) {
return /PLC/.test(item);
});
// let autocomplete know the results:
response(matches);
}
});
http://jsfiddle.net/GarethPN/xbZhr/6/
But how would I use the term variable in the regular expression to retain the standard functionality?
Or is there a blindingly obvious easier way that I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个想法是创建一个匹配结果数组并向其中添加 PLC:
在此处进行操作: http://jsfiddle.net /xbZhr/8/
One idea is to create an array of matching result and adding PLC to them:
fiddle here: http://jsfiddle.net/xbZhr/8/