Trigger jQuery Autocomplete manually

你。 提交于 2019-12-23 06:57:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!