Javascript 手动触发 jQuery 自动完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13686319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:40:16  来源:igfitidea点击:

Trigger jQuery Autocomplete manually

javascriptjqueryajaxsearchautocomplete

提问by TheFrack

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).

我正在使用带有一些 AJAX 的 jQuery UI 自动完成功能(直到他们停止输入后才会提取数据)。我想这样做,一旦找到数据,自动完成就会作为搜索结果弹出。这有效,但是只有当我再次开始输入时(下拉菜单在我输入之前不会触发,因为它直到我停止输入后才被初始化)。

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.

最后一部分是尝试激活自动完成,但失败了。

回答by Thorsten

The search methodshould do the trick:

搜索方法应该做的伎俩:

$('input#mainSearchBox').autocomplete("search");

Fiddle

小提琴

回答by jbabey

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 exampleon the jquery autocomplete demo site (lines 127-141).

此代码可以在 jquery 自动完成演示站点上的组合框示例的源代码中找到(第 127-141 行)。

回答by Salma Gomaa

For https://github.com/devbridge/jQuery-Autocomplete, you can use:

对于https://github.com/devbridge/jQuery-Autocomplete,您可以使用:

$('input#mainSearchBox').autocomplete("getSuggestions", $('input#mainSearchBox').val())