jQuery UI 自动完成使用 startsWith [英] jQuery UI Autocomplete use startsWith

查看:36
本文介绍了jQuery UI 自动完成使用 startsWith的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有本地数据源的 jQuery UI 自动完成功能 (source: myArray).我希望自动完成功能仅建议以开头输入的字符串的结果,而不是默认的不区分大小写的包含搜索.是否有一个简单的解决方案,或者我是否必须提供我的自定义搜索/源回调?

I'm using the jQuery UI Autocomplete with a local datasource (source: myArray). I want the autocomplete to propose only the results that start with the string entered instead of the default case-insensitive contains search. Is there a simple solution for this or do I have to provide my custom search/source callback?

推荐答案

目前我是这样做的,不知道有没有更好的解决方案:

Currently I've done it this way, not sure if there is a better solution:

source: function(request, response) {
    var filteredArray = $.map(orignalArray, function(item) {
        if( item.value.startsWith(request.term)){
            return item;
        }
        else{
            return null;
        }
    });
    response(filteredArray);
},

这种方法还可以对要显示的项目数量施加限制(例如 10 个项目).

This approach also made it possible to impose a limit (e.g. 10 items) on the amount of items to be shown.

这篇关于jQuery UI 自动完成使用 startsWith的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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