jQuery autocomplete 1.1:聚焦显示所有数据

jQuery autocomplete 1.1: Show All Data on focus

本文关键字:显示 数据 聚焦 autocomplete jQuery      更新时间:2023-09-26

如何使此扩展集中显示所有数据?。我曾尝试将minChars更改为0,但它只在双击输入时显示。

$("#month").autocomplete(months, {
        minChars: 0,
        max: 12,
        autoFill: true,
        mustMatch: true,
        matchContains: false,
        scrollHeight: 220,
        formatItem: function(data, i, total) {
            // don't show the current month in the list of values (for whatever reason)
            if ( data[0] == months[new Date().getMonth()] ) 
                return false;
            return data[0];
        }
    });

您需要将焦点事件绑定到输入,并因此调用jQuery UI方法。看看这个js fiddle作为的例子

我添加了以下代码:

$('#month').autocomplete({
    // Your current code
}).on('focus', function(event) {
    var self = this;
    $(self).autocomplete( "search", this.value);;
});

传递到CCD_ 2方法中的值是自动完成将要查找的值。

如何搜索焦点上的所有值

如果您想要所有可用的下拉列表,请将其保留为",但将minLength : 0添加到选项对象中。

$('#month').autocomplete({
    minLength : 0
}).on('focus', function(event) {
    $(this).autocomplete("search", "");
});

我最近也遇到了同样的问题,因为这个插件很旧(而且不推荐使用(,所以这个版本的文档很少。

这对我有效:

var __n_clicks = 0;
$("#autocomplete_input").autocomplete(data, {
    minChars: 0,
    ...
}).on('click', function(event) {
    if(__n_clicks < 1){
        __n_clicks++;
        $(this).click();
    }else {
        __n_clicks = 0;   
    }
});

执行"dblclick"也不起作用,必须单击2次。

如果你想用这个插件创建一些组合框,试试这个:

$('.lookup').on('click', function(event) {
var str =$(this).attr('id');
$('#'+str.slice(1)).focus().click().click();
});

    $(".autocomplete_input").autocomplete(availableTags, {
        minChars: 0,
    }).focus(function(event) {
        $(this).click();
    });

这也可以只点击一次,但最好只是完全切换插件。