admin管理员组

文章数量:1187660

How do I enable exact match from start of string using jquery autocomplete with input from simple array?

If i have the following in an array:

  • smart
  • oversmart
  • smartland
  • undersmart
  • verysmart

And if I am typing "sma..." in the text input, i must be shown only smart and smartland, not the others.

How do I enable exact match from start of string using jquery autocomplete with input from simple array?

If i have the following in an array:

  • smart
  • oversmart
  • smartland
  • undersmart
  • verysmart

And if I am typing "sma..." in the text input, i must be shown only smart and smartland, not the others.

Share Improve this question asked May 5, 2012 at 17:13 VKVKVKVK 1311 gold badge1 silver badge5 bronze badges 1
  • possible duplicate of jQuery Autocomplete plug-in search configuration – j08691 Commented May 5, 2012 at 17:18
Add a comment  | 

5 Answers 5

Reset to default 16

You just need to modify source parameter as a function to suit your needs. Like this:

http://jsfiddle.net/UKgD6/


Update: Adding code to answer:

var acList = ['smart', 'oversmart', 'smartland', 'undersmart', 'verysmart'];
$('#ac').autocomplete({
    source: function (request, response) {
        var matches = $.map(acList, function (acItem) {
            if (acItem.toUpperCase().indexOf(request.term.toUpperCase()) === 0) {
                return acItem;
            }
        });
        response(matches);
    }
});

The way to do this is documented at http://api.jqueryui.com/autocomplete/

<script>
    var tags = [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ];
    $( "#autocomplete" ).autocomplete({
      source: function( request, response ) {
              var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
              response( $.grep( tags, function( item ){
                  return matcher.test( item );
              }) );
          }
    });
</script>

You can use regular expressions to match the entered substring with the values in the array you have.

JQuery has way too many plug-ins. Just sprinkle a little regEx on it and write something like this (I didn't test it but I've done similar stuff before):

$('#someTextInput').keyup( function(){
    var regExMatch = new RegEx( '^' + $(this).val() );
    $_LIs = $('#someUl li');
    $_LIs.hide();
    $_LIs.each( function(){
        if( this.innerText.match(regExMatch) ) { $(this).show(); }
    } );
} );

You can remove the '^' + in the new RegEx parameters to restore the behavior you're describing as problematic. '^' signifies beginning of a line/string selection so 's' won't match unless it's at the start.

This has changed since the previous answer was accepted. There is now a lookupFilter option to do this and it is much simpler. From https://stackoverflow.com/a/23239873/1204434, just add this to your list of options:

lookupFilter: function (suggestion, originalQuery, queryLowerCase) {
                return suggestion.value.toLowerCase().indexOf(queryLowerCase) === 0;
            },

本文标签: javascriptauto complete exact match from start using jquery autocomplete from simple arrayStack Overflow