简体   繁体   中英

Jquery autocomplete “search” method

I am using Jquery UI autocomplete, this is the code below

var opt_source = {...}
var options = {
            minLength: 0,
            source: opt_source,
            search: "aPreDefinedString"
        };
$(".searchable_input").autocomplete(options);

My understanding is that it should now search for aPreDefinedString ; This doesn't happen, rather it searches the local source for userInput . Could someone point out where I am going wrong?

OK, this is how I had to make it work

var opt_source = {..};

var options = {
            minLength: 0,
            source: function(request, response){
                response(opt_source);
            }
        };
$(".searchable_input").autocomplete(options);

This seems to override the inbuilt search (I hope they dont break it in future versions)

From the Jquery UI documentation

The third variation, the callback, provides the most flexibility, and can be used to connect any data source to Autocomplete. The callback gets two arguments:

A request object, with a single property called "term", which refers to the value currently in the text input. For example, when the user entered "new yo" in a city field, the Autocomplete term will equal "new yo". A response callback, which expects a single argument to contain the data to suggest to the user.

This data should be filtered based on the provided term, and can be in any of the formats described above for simple local data (String-Array or Object-Array with label/value/both properties). It's important when providing a custom source callback to handle errors during the request. You must always call the response callback even if you encounter an error. This ensures that the widget always has the correct state

I think you're mixing up the search event and the search method on the autocomplete widget. You can specify an event handler for the search event in the options object (which you're doing) that's used to initialize the widget.

The way you would call the search method is like this:

$(".searchable_input").autocomplete( "search" , "aPreDefinedString" );

This would manually search the autocomplete.

my code is like this and it work.

var availableTags = ['aa','bb','cc'];

$( "#filterinput" ).autocomplete({
                      source: availableTags,
                      autoFocus: true,
                    });

$( "#filterinput" ).on( "autocompletesearch", function( event, ui ) {
                      console.log($(this).val());
                    } );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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