0

I'm working on a new local project where I have lots of street names added to my database. I'm pulling these for making the autocomplete script work. Everything is good so far, however, I need to make 2 modifications to my existing script.

First one: adding the matcher.slice(0,10) to work with my current code // to make sure only 10 suggestions are shown at a time Second one: blocking free typing in the input field. So if the street name is Čačanska, block new entries like Čačanskablah. jsfiddler examples added below

<script>
    $(function() {
var names = [ "Čačanska", "Čajetinska", "Čantavirski Put", "Čapajeva", "Čavoljska", "Čede Žice", "Čikerijska", "Đakovska", "Đerđa Dože", "Đerđa Lukača", "Đerda Dože", "Đerdapska", "Đeri Ferenca", "Đevđelijska", "Đorđa Natoševića", "Đorda Natoševica", "Đule Valija", "Đurđinska", "Đurdinska", "Đure Đakovića", "Đure Daničića", "Đure Stantića", "Šabačka", "Šajkaška", "Šamačka", "Šandora Lifke", "Šandora Petefija", "Šantićeva", "Šarplaninska", "Šebešićka", "Šekspirova", "Šenoina", "Šibenska", "Šidska", "Šime Ivića", "Šime Tikvickog", "Šimeta Tikvickog", "Široki Progon" ];

 var accentMap = {
            "š": "s",
            "ć" : "c",
            "č" : "c",
            "ž" : "z",
            "đ" : "d",
            "s" : "š",
            "c" : "č",
            "c" : "ć",
            "z" : "ž",
            "d" : "đ",
            "Š": "S",
            "Ć" : "C",
            "Č" : "C",
            "Ž" : "Z",
            "Đ" : "D",
            "S" : "Š",
            "C" : "Č",
            "C" : "Ć",
            "Z" : "Ž",
            "D" : "Đ"
        };
        var normalize = function( term ) {
            var ret = "";
            for ( var i = 0; i < term.length; i++ ) {
                ret += accentMap[ term.charAt(i) ] || term.charAt(i);
            }
            return ret;
        };

        $( "#addr" ).autocomplete({
            source: function( request, response ) {
                var matcher = new RegExp( $.ui.autocomplete.escapeRegex( request.term ), "i" );
                response( $.grep( names, function( value ) {
                    value = value.label || value.value || value;
                    return matcher.test( value ) || matcher.test( normalize( value ) );
                }));

                }
        });
    });
</script>

First one example: http://jsfiddle.net/andrewwhitaker/vqwBP/

Second one example: http://jsfiddle.net/prCsm/

so basically, I would like to add both examples to my existing code, but no matter what I do, the autocomplete stops working.. thanks in advance!

4

1 回答 1

0

在等待有人帮助我时,我仍在尝试自己解决...

“一次显示 10 个结果”的解决方案如下

$( "#addr" ).autocomplete({
    source: function( request, response ) {
        var matcher = new RegExp( $.ui.autocomplete.escapeRegex( request.term ), "i" );
        var results = $.grep( names, function( value ) {
            value = value.label || value.value || value;
            return matcher.test( value ) || matcher.test( normalize( value ) );
                       })
            results = results.slice(0,10);
        response( results );
        }
});
于 2012-09-24T15:20:15.917 回答