jQuery自动完成UI-我希望它开始搜索onfocus而无需用户输入任何内容

sdf*_*for 8 jquery jquery-ui-autocomplete

jQuery自动完成UI - 我想开始搜索"onfocus",并在用户选中或点击进入搜索字段时立即显示选项列表,而无需用户输入任何内容.

默认行为似乎要求用户输入字符或向下箭头以开始滚动并开始搜索并获取值,即使我将所需字符数设置为零也是如此.


$( "#contact" ).autocomplete({
    source: 'remote.php',
    minLength: 0
});

谢谢!

Dun*_*unc 13

比艾美特的答案复杂一点,但......

  • 即使框已包含文本,也会在焦点上弹出列表
  • 单击项目后避免重新弹出列表

这里是:

var closing = false;

$('#contact').autocomplete({
    source: 'remote.php',
    minLength: 0,
    close: function()
    {
        // avoid double-pop-up issue
        closing = true;
        setTimeout(function() { closing = false; }, 300);
    }
})
.focus(function() {
    if (!closing)
        $(this).autocomplete("search");
});
Run Code Online (Sandbox Code Playgroud)


小智 6

我发现这段代码更清晰,特定于元素.

 $(<selector>).autocomplete({
            minLength: 0,
            delay: 500,
            source: funcDataLookUp,
            open: function() { $(this).attr('state', 'open'); },
            close: function () { $(this).attr('state', 'closed'); }
        }).focus(function () {
            if ($(this).attr('state') != 'open') {
                $(this).autocomplete("search");
            }
        });
Run Code Online (Sandbox Code Playgroud)


Pra*_*een 5

尝试将focus与自动完成绑定。

$("#contact").autocomplete({
        source: 'remote.php',
        minLength: 0
    }).bind('focus', function () {
        $(this).autocomplete("search");
    });
Run Code Online (Sandbox Code Playgroud)

查看我的示例JSFiddle


Emm*_*ett 4

$("#contact").focus(function() {
    if ($(this).val().length == 0) {
        $(this).autocomplete("search");
    }
});
Run Code Online (Sandbox Code Playgroud)

确保您的自动完成minLength值为 0。