问题
I'm using jQuery UI Autocomplete with some AJAX (the data isn't pulled until after they stop typing). I would like to make it so once the data is found, Autocomplete will then pop-up as a search result. This works, however only when I start typing again (the dropdown doesn't trigger until I type because it's not initialized until after I stop typing).
My code:
var availableTags = [
"Perl",
"PHP",
"Python",
"Ruby"
];
$('input#mainSearchBox').autocomplete({
source: availableTags,
minLength: 0
});
$('input#mainSearchBox').data('autocomplete').menu.active;
The last part was an attempt to activate autocomplete, but it fails.
回答1:
The search method should do the trick:
$('input#mainSearchBox').autocomplete("search");
Fiddle
回答2:
You can use the following script to toggle the autocomplete manually:
var textbox = $('input#mainSearchBox');
var autocompleteBox = textbox.autocomplete('widget');
// toggle the autocomplete widget
autocompleteBox.is(':hidden') ?
textbox.autocomplete('search', textbox.val()).focus() :
autocompleteBox.hide();
This code can be found in the source of the combobox example on the jquery autocomplete demo site (lines 127-141).
回答3:
For https://github.com/devbridge/jQuery-Autocomplete, you can use:
$('input#mainSearchBox').autocomplete("getSuggestions", $('input#mainSearchBox').val())
来源:https://stackoverflow.com/questions/13686319/trigger-jquery-autocomplete-manually