jQuery-自动完成过滤器按首字母匹配 [英] jQuery-autocomplete filter match by first letter

查看:15
本文介绍了jQuery-自动完成过滤器按首字母匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下插件来创建自动完成功能:

I am using below plugin to create autocomplete feature:

https://github.com/devbridge/jQuery-Autocomplete

以下是他们的现场演示:

below is their live demo:

http://www.devbridge.com/sourcery/components/jquery-autocomplete/#jquery-autocomplete

现在的问题是,我想过滤结果并只显示以第一个字母开头的字母匹配.

now the problem is, I'd like to filter the result and only show letter match start with first letter.

例如,如果我输入a",则只会出现以a"开头的国家/地区.

for example, if I typed into 'a', only countries start with 'a' should appear.

我查看了这个插件的库,看到里面有一个lookupFilter函数,但是不知道怎么用.

I checked this plugin's library, I saw there is a lookupFilter function there, but don't know how to use it.

之前用过这个库的人能指点一下吗?

would anyone who had used this library before kindly give some pointers?

我想使用这个插件而不是 jQuery UI,因为它是轻量级的.

I'd like to use this plugin instead of jQuery UI, as it is light weighted.

推荐答案

我在他们的库中检查了 lookupFilter,下面是代码:

I checked lookupFilter in their library and below is the code:

lookupFilter: function (suggestion, originalQuery, queryLowerCase) {
    return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
},

在这里,如果结果中存在该值,他们基本上会返回该值.

Here they are returning the value bascially if it is present in the results.

-1 表示未找到字母 a,因此它们在任何出现时都返回该值.

-1 means the letter a is not found, so they're returning the value at any occurrence.

所以只需将其调整为:

lookupFilter: function (suggestion, originalQuery, queryLowerCase) {
    return suggestion.value.toLowerCase().indexOf(queryLowerCase) === 0;
},

正好在第 0 个索引处.

这将返回第一次出现 a 的结果.

This will return the results with a as first occurance.

这篇关于jQuery-自动完成过滤器按首字母匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆