开发者

jQuery UI Autocomplete ignore non alphanumeric letters

开发者 https://www.devze.com 2023-04-07 17:40 出处:网络
I want to ignore all non alphanumeric letters from autocomplete input. For example if user inputs K P COLLECTION it can search K. P. COLLECTION.

I want to ignore all non alphanumeric letters from autocomplete input. For example if user inputs K P COLLECTION it can search K. P. COLLECTION.

This is my code on http://jsbin.com/usupem

Code:

var autocomplete_data = data here...

$( ".autocomplete" ).autocomplete({
    source: function(req, response) {
        var re = $.ui.autocomplete.escapeRegex(req.term);
        var matcher = new RegExp( "^" + re, "i" );
        response($.grep(autocomplete_data, function(item){return matcher.test(item.label); }) );
    },
    focus: function( event, ui ) {
            $( ".autocomplete" ).val( ui.item.label );
       开发者_JS百科     return false;
    },
    select: function( event, ui ) {
            $( ".autocomplete" ).val( ui.item.label );
            return false;
    }
}).data( "autocomplete" )._renderItem = function( ul, item ) {
        return $( "<li></li>" ).data( "item.autocomplete", item ).append( "<a>" + item.label + "<br><small>" + item.desc + "</small></a>" ).appendTo( ul );
};


You should strip the non alpha-numeric characters from both the input and the term that you're matching. Try calling something like this on both the req.term and item.label values in your source function:

function stripNonAlphaNumeric(string){
  var r = string.toLowerCase();
  r = r.replace(new RegExp("[^A-z0-9 ]", 'g'), "");
  return r;

}

http://jsbin.com/ufetiq/3

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号