简体   繁体   English

jQuery自动完成推荐

[英]Jquery auto complete recommendation

I am trying JQuery auto complete example. 我正在尝试使用JQuery自动完成示例。 Please see - http://jsfiddle.net/LCv8L/797/ 请参阅-http://jsfiddle.net/LCv8L/797/

When I type "Ar" it should give "Ar" related item first. 当我键入“ Ar”时,应首先输入“ Ar”相关项。 But it shows "An" first though I have given minLength 1. May be it is considering alphabetical order. 尽管我给了minLength 1,但是它首先显示了“ An”。可能是考虑了字母顺序。 Is there any suggestion to solve this issue? 有解决这个问题的建议吗?

    minLength: 1,
    select: function(event, ui) {
        event.preventDefault();
        $("#customer-search").val(ui.item.label);
        $("#selected-customer").val(ui.item.label);
    },
    focus: function(event, ui) {
        event.preventDefault();
        $("#customer-search").val(ui.item.label);
    }
});

By default Autocomplete search items that contain string, not starts with. 默认情况下,包含字符串而不是开头的自动完成搜索项。

$('#customer-search').autocomplete({
    source: function(request, response) {
        var filtered = $.map(data, function(item) {
            if(item.value.toLowerCase().startsWith(request.term.toLowerCase())) {
                return item;
            }
            else {
                return null;
            }
        });
        response(filtered);
    }
});

Try to change the Regular expression in namespace in the "search" method. 尝试在“搜索”方法中更改名称空间中的正则表达式。

Please find answer in jsfiddle.net/LCv8L/800/ 请在jsfiddle.net/LCv8L/800/中找到答案

Run this code in your jsfiddle it is working as you want.. :) //use regular expression is better way to prevent this. 在您的jsfiddle中运行此代码,即可按需运行。.::) //使用正则表达式是防止这种情况的更好方法。

   var wordlist= [
       "Afghanistan",
        "Albania",
        "Algeria",
        "American Samoa",
       "Andorra",
        "Angola",
        "Anguilla",
        "Antarctica",
        "Antigua and Barbuda",
        "Argentina",
        "Armenia",
        "Aruba",
        "Australia",
        "Austria",
        "Azerbaijan"
    ] ; 

    $("#customer-search").autocomplete({

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM