【问题标题】:jQuery UI Autocomplete only popping up with minLength: 0jQuery UI 自动完成仅弹出 minLength: 0
【发布时间】:2012-05-13 00:32:20
【问题描述】:

我在http://jqueryui.com/demos/autocomplete/ 使用了“自定义数据和显示”示例并对其进行了一些简化,以便我可以拥有如下值和 ID:

$(function() {
    var prd = [
        { s: "hallo", v: "1" },
        { s: "hey", v: "2" },
        { s: "alloh", v: "3" }
    ];
    $("#product").autocomplete({
        source: prd,
        minLength: 0,
        focus: function(event, ui) {
            $("#product").val(ui.item.s);
            return false;
        },
        select: function(event, ui) {
            $("#product").val(ui.item.s);
            $("#productId").val(ui.item.v);
            return false;
        }
    }).data("autocomplete")._renderItem = function(ul, item) {
        return $("<li></li>")
            .data("item.autocomplete", item)
            .append("<a>" + item.s + "</a>")
            .appendTo(ul);
    };
});

问题是如果没有 minLength:0 它永远不会弹出,而使用 minLength:0 它只会弹出一个空文本框(输入一个字符然后删除它)。

【问题讨论】:

    标签: jquery jquery-ui autocomplete jquery-ui-autocomplete


    【解决方案1】:

    Jqueryui 自动完成对“标签”和“值”的使用几乎是自动的。将“s”和“v”改回“label”和“value”,一切都会按预期工作:

      <script>
        $(function() {
            var prd = [
                { label: "hallo", value: "1" },
                { label: "hey", value: "2" },
                { label: "alloh", value: "3" }
            ];
            $("#product").autocomplete({
                source: prd,
                minLength: 0,
                focus: function(event, ui) {
                    $("#product").val(ui.item.label);
                    return false;
                },
                select: function(event, ui) {
                    $("#product").val(ui.item.label);
                    $("#productId").val(ui.item.value);
                    return false;
                }
            }).data("autocomplete")._renderItem = function(ul, item) {
                return $("<li></li>")
                    .data("item.autocomplete", item)
                    .append("<a>" + item.label + "</a>")
                    .appendTo(ul);
            };
        });
      </script>
    

    【讨论】:

      猜你喜欢
      • 2011-06-04
      • 1970-01-01
      • 2017-12-23
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      相关资源
      最近更新 更多