jQuery UI Autocomplete Multiple Values in Textbox

前端 未结 2 1303
感动是毒
感动是毒 2020-12-30 12:58

I need a simple autocomplete search functionality but also allowing users to type more than one value. I\'m using jQuery UI\'s autocomplete widget (http://jqueryui.com/autoc

2条回答
  •  天命终不由人
    2020-12-30 13:34

    Try this:

      function split( val ) {
        return val.split( /,\s*/ );
      }
    
      function extractLast( term ) {
         return split( term ).pop();
       }
    
       $( "#search" )
            .autocomplete({
                 minLength: 0,
                 source: function( request, response ) {
                     response( $.ui.autocomplete.filter(
                         items, extractLast( request.term ) ) );
                 },
                 focus: function() {
                     return false;
                 },
                select: function( event, ui ) {
                    var terms = split( this.value );
                    terms.pop();
                    terms.push( ui.item.value );
                    terms.push( "" );
                    this.value = terms.join( ", " );
                    return false;
                }
            });
    

    SEE DEMO

提交回复
热议问题