使用 jQuery UI 自动完成,如何确保无论输入什么文本都会出现一个特定的结果?

发布于 2024-12-05 03:00:53 字数 922 浏览 1 评论 0原文

我的自动完成源列表有点像这样:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖公主 2024-12-12 03:00:53

一个想法是创建一个匹配结果数组并向其中添加 PLC:

var items = ['UK', 'IE', 'FR', 'BE', 'NL'];
$("#autocomplete").autocomplete({
    source: function(request, response) {
        // The term the user searched for;
        var term = request.term;

        var matches = [];
        matches.push('PLC');/(add PLC
        // Extract matching items: (write your own function here)


        // let autocomplete know the results:
        response(matches);
    }
});

在此处进行操作: http://jsfiddle.net /xbZhr/8/

One idea is to create an array of matching result and adding PLC to them:

var items = ['UK', 'IE', 'FR', 'BE', 'NL'];
$("#autocomplete").autocomplete({
    source: function(request, response) {
        // The term the user searched for;
        var term = request.term;

        var matches = [];
        matches.push('PLC');/(add PLC
        // Extract matching items: (write your own function here)


        // let autocomplete know the results:
        response(matches);
    }
});

fiddle here: http://jsfiddle.net/xbZhr/8/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文