改变自动完成搜索的工作方式

发布于 2024-11-25 08:24:45 字数 235 浏览 2 评论 0原文

目前我有:

$("#location").autocomplete({source: cities, minLength: 0, autoFocus: true});

问题是这个插件在给定输入中搜索任何地方的匹配项。

例如,如果我输入“Bos”,它不仅会建议以“Bos”开头的单词,还会建议中间有“bos”的单词。

我该如何解决这个问题?

Currently I have:

$("#location").autocomplete({source: cities, minLength: 0, autoFocus: true});

The problem is that this plugin searches for matches anywhere inside the given inputs.

For example, if I enter "Bos" it suggests not only the words that start with "Bos", but also the words that have "bos" in the middle.

How can I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

抱着落日 2024-12-02 08:24:45

看起来您必须使用 source 选项的回调设置:

第三种变体,即回调,提供了最大的灵活性,并且
可用于将任何数据源连接到自动完成。回调
获取两个参数:

  • 一个请求对象,有一个名为“term”的属性,它
    指当前文本输入中的值。例如,当
    用户在城市字段中输入“new yo”,自动完成术语将
    等于“新哟”。
  • 响应回调,它期望包含一个参数
    向用户建议的数据。应根据以下条件过滤此数据
    提供的术语,可以采用上述任何格式
    对于简单的本地数据(字符串数组或对象数组
    标签/值/两个属性)。提供定制时很重要
    用于处理请求期间错误的源回调。你必须始终
    即使遇到错误,也会调用响应回调。这
    确保小部件始终具有正确的状态。 (来源

未经测试:

$(function() {
    var options = ["Foo", "Bar", "Baz", "Foobar"];

    $(<selector>).autocomplete({
        source: function(request, response) {
                    var matches = [],
                        term = request.term.toLowerCase(),
                        termLen = term.length;

                    for (var i = 0, j = options.length; i < j; i++) {
                         if (options[i].substring(0, termLen).toLowerCase() == term) {
                             matches.push(options[i]);
                         }
                    }

                    response(matches);
            }
    });
});

Looks like you'll have to use the callback setting for the source option:

The third variation, the callback, provides the most flexibility, and
can be used to connect any data source to Autocomplete. The callback
gets two arguments:

  • A request object, with a single property called "term", which
    refers to the value currently in the text input. For example, when the
    user entered "new yo" in a city field, the Autocomplete term will
    equal "new yo".
  • A response callback, which expects a single argument to contain
    the data to suggest to the user. This data should be filtered based on
    the provided term, and can be in any of the formats described above
    for simple local data (String-Array or Object-Array with
    label/value/both properties). It's important when providing a custom
    source callback to handle errors during the request. You must always
    call the response callback even if you encounter an error. This
    ensures that the widget always has the correct state. (source)

Untested:

$(function() {
    var options = ["Foo", "Bar", "Baz", "Foobar"];

    $(<selector>).autocomplete({
        source: function(request, response) {
                    var matches = [],
                        term = request.term.toLowerCase(),
                        termLen = term.length;

                    for (var i = 0, j = options.length; i < j; i++) {
                         if (options[i].substring(0, termLen).toLowerCase() == term) {
                             matches.push(options[i]);
                         }
                    }

                    response(matches);
            }
    });
});
海风掠过北极光 2024-12-02 08:24:45

这在 jQuery Autocomplete 插件中很容易实现,其文档回答了这个问题:

matchContains | 布尔值 | 默认值: false

是否比较
查看搜索结果内部(即“ba”是否匹配“foo bar”)。
仅当您使用缓存时才重要。不要与自动填充混合。

您可能希望从 jQuery UI Autocomplete 切换到此功能,因为它功能更齐全。

This is easy in the jQuery Autocomplete plugin, whose documentation answers this:

matchContains | Boolean | Default: false

Whether or not the comparison
looks inside (i.e. does "ba" match "foo bar") the search results.
Important only if you use caching. Don't mix with autofill.

You may wish to switch to this from jQuery UI Autocomplete as it's more fully-featured.

向地狱狂奔 2024-12-02 08:24:45

正如 @jensgram 所说,您可以使用 source 方法。

在我的网络应用程序中,类似的东西对我有用:

$("#location").autocomplete({
    source : function(request, response) {
        /* cities.html?query=... should return json array words starting with query value */
        $.getJSON('cities.html?query=' + request.term, function(data) {
            response(data);
        });
    }
});

As @jensgram said You could use source method.

Something like that works for me in my web-app:

$("#location").autocomplete({
    source : function(request, response) {
        /* cities.html?query=... should return json array words starting with query value */
        $.getJSON('cities.html?query=' + request.term, function(data) {
            response(data);
        });
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文