如何在输入时过滤 jquery 自动完成数据

发布于 2024-12-09 12:03:17 字数 689 浏览 0 评论 0原文

到目前为止,我有以下内容

var aCleanData = ['aaa','aab','faa','fff','ffb','fgh','mmm','maa'];

$('#my-input').autocomplete({
    source:aCleanData,
    minLength:2
});

当前如果您输入 aa,将显示 aaa,aab,faa,maa

我想做的是,当用户输入 ff 时,显示的数据将是 fff,ffb 数据。

基本上,只有输入的内容才应该从第一个字符开始匹配。

这应该是递归的。当用户输入 fg 时,fff,ffb 应消失,仅应出现 fgh

预先感谢您的帮助。

更新:

ps 看看我的意思:

http://jqueryui.com/demos/autocomplete/#default< /a>

输入 sc,您将看到的不仅仅是以 sc 开头的数据。

So far I have the following

var aCleanData = ['aaa','aab','faa','fff','ffb','fgh','mmm','maa'];

$('#my-input').autocomplete({
    source:aCleanData,
    minLength:2
});

Currently if you type aa, aaa,aab,faa,maa will show.

What I would like to do is when the user types ff the data that is shown will be the fff,ffb data.

Basically, only what is typed should be matched from the first character onwards.

This should be recursive. When user types fg, fff,ffb should disapear and only fgh should appear.

Thanks in advance for your help.

UPDATE:

p.s. See what I mean here:

http://jqueryui.com/demos/autocomplete/#default

Type sc and you'll see more than just the data beginning with sc.

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

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

发布评论

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

评论(4

铃予 2024-12-16 12:03:18

查看自动完成的源代码后,您有几个选择。您可以编写自己的 pasrer 方法来返回您需要的内容并将其设置为回调源。这可能是更“正确”的方法。

更快的方法是在包含 ui 源代码后简单地添加以下行:

$.ui.autocomplete.escapeRegex=function(){ 
    return '[^|\s]' + $value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\
amp;");   
};

如果您关心它是如何工作的(或者应该工作,我没有测试过):

原始代码使用 2 个静态函数扩展了 ui.autocomplete:

$.extend( $.ui.autocomplete, {
escapeRegex: function( value ) {
    return value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\
amp;");
},
filter: function(array, term) {
    var matcher = new RegExp( $.ui.autocomplete.escapeRegex(term), "i" );
    return $.grep( array, function(value) {
        return matcher.test( value.label || value.value || value );
    });
}
});

所有你应该需要做的是更改 escapeRegex 返回的内容以仅搜索开头的单词。通过将 escapeRegex 的值设置为在原始返回前面返回 '[^|\s]' ,我们的意思是“查找前面有空格或者是行开头的工作” ”

After looking at the source for autocomplete, you have a few options. You could write your own pasrer method that returns what you need and set it as the callback source. This is probably the more "correct" way to do it.

The faster way is to simply add the following line AFTER you include the ui source:

$.ui.autocomplete.escapeRegex=function(){ 
    return '[^|\s]' + $value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\
amp;");   
};

If you care how this works (or should work, I have not tested):

The original code extends the ui.autocomplete with 2 static functions:

$.extend( $.ui.autocomplete, {
escapeRegex: function( value ) {
    return value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\
amp;");
},
filter: function(array, term) {
    var matcher = new RegExp( $.ui.autocomplete.escapeRegex(term), "i" );
    return $.grep( array, function(value) {
        return matcher.test( value.label || value.value || value );
    });
}
});

All you should need to do is change what escapeRegex returns to search for the beginning if words only. By setting the value of escapeRegex to return '[^|\s]' in front of the original return, we are saying "Look for the work with a space in front or is the beginning of a line"

行至春深 2024-12-16 12:03:18

我认为您使用jQuery UI - 自动完成。单一搜索类型与您想要的方式不同。但是这个可能可以满足您的需求。

I think you use the jQuery UI - Autocomplete. The one searching type is not the same way that you want to do. But this one is may be cover for your need.

余厌 2024-12-16 12:03:18

您可以使用此技术的简单方法:

    const autocompleteFilter = function( request, response ) {
            var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
            response( $.grep( <array>, function( item ){
                return matcher.test( item );
            }) );
        };

    $("#From").autocomplete({
        source: autocompleteFilter,
        select: function(event, selectedItem) {
            selectedItem = splitValue(selectedItem.item.value, 3);
            if (selectedItem) {
                return selectedItem.text;
            } else 
                return false;
        },
    });

Simply way you can use this technique :

    const autocompleteFilter = function( request, response ) {
            var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
            response( $.grep( <array>, function( item ){
                return matcher.test( item );
            }) );
        };

    $("#From").autocomplete({
        source: autocompleteFilter,
        select: function(event, selectedItem) {
            selectedItem = splitValue(selectedItem.item.value, 3);
            if (selectedItem) {
                return selectedItem.text;
            } else 
                return false;
        },
    });
不弃不离 2024-12-16 12:03:17

仅获取以输入值开头的结果的一种可能的解决方案是在自行搜索之前检查数组元素:

var aCleanData = ['aaa','aab','faa','fff','ffb','fgh','mmm','maa'];
$('#my-input').autocomplete({
    source: aCleanData,
    minLength: 2,
    search: function(oEvent, oUi) {
        // get current input value
        var sValue = $(oEvent.target).val();
        // init new search array
        var aSearch = [];
        // for each element in the main array ...
        $(aCleanData).each(function(iIndex, sElement) {
            // ... if element starts with input value ...
            if (sElement.substr(0, sValue.length) == sValue) {
                // ... add element
                aSearch.push(sElement);
            }
        });
        // change search array
        $(this).autocomplete('option', 'source', aSearch);
    }
});

另请参阅我的 jsfiddle< /a>.

One possible solution to get only results starting with the input value is to check the array elements before search by yourself:

var aCleanData = ['aaa','aab','faa','fff','ffb','fgh','mmm','maa'];
$('#my-input').autocomplete({
    source: aCleanData,
    minLength: 2,
    search: function(oEvent, oUi) {
        // get current input value
        var sValue = $(oEvent.target).val();
        // init new search array
        var aSearch = [];
        // for each element in the main array ...
        $(aCleanData).each(function(iIndex, sElement) {
            // ... if element starts with input value ...
            if (sElement.substr(0, sValue.length) == sValue) {
                // ... add element
                aSearch.push(sElement);
            }
        });
        // change search array
        $(this).autocomplete('option', 'source', aSearch);
    }
});

Also see my jsfiddle.

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