jQuery UI 自动完成 - 从字符串的开头搜索

发布于 2024-12-10 04:42:49 字数 596 浏览 0 评论 0原文

我想将 Jquery UI 插件设置为仅从字符串的开头进行搜索。我发现一些类似的问题,但没有可用的答案。 我有这个代码:

$.ajax({
        url: "{!$basePath}/mesta.xml",
        dataType: "xml",
        success: function( xmlResponse ) {
            var data = $( "city", xmlResponse ).map(function() {
                return {
                    value: $( "name", this ).text(),
                    id: $( "name", this ).text()
                };
            }).get();
            $( ".autocomplete" ).autocomplete({
                source: data,
                minLength: 0
            });
        }
    });

谢谢。

I want to set Jquery UI plugin to search only from the beginning of the string. I find some simillar questions but no usable answer.
I have this code:

$.ajax({
        url: "{!$basePath}/mesta.xml",
        dataType: "xml",
        success: function( xmlResponse ) {
            var data = $( "city", xmlResponse ).map(function() {
                return {
                    value: $( "name", this ).text(),
                    id: $( "name", this ).text()
                };
            }).get();
            $( ".autocomplete" ).autocomplete({
                source: data,
                minLength: 0
            });
        }
    });

Thank you.

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

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

发布评论

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

评论(1

倾城°AllureLove 2024-12-17 04:42:49

只需将 @Samich 发布的链接中的内容应用到您的 source 选项即可:

source: function(req, response) { 
    var re = $.ui.autocomplete.escapeRegex(req.term); 
    var matcher = new RegExp( "^" + re, "i" ); 
    response($.grep( data, function(item){ 
        return matcher.test(item.value); }) ); 
 },

这是一个小提琴演示:http://jsfiddle.net/jensbits/PekQZ/

Just apply what is in the link @Samich posted to your source option:

source: function(req, response) { 
    var re = $.ui.autocomplete.escapeRegex(req.term); 
    var matcher = new RegExp( "^" + re, "i" ); 
    response($.grep( data, function(item){ 
        return matcher.test(item.value); }) ); 
 },

And here is a fiddle demo: http://jsfiddle.net/jensbits/PekQZ/

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