小智 24

我尝试以我自己的项目中的方式回答这个问题.

我从您发布的页面中读取了演示源代码.在生成自动完成组合框的jquery代码中,我添加了一行代码,这些代码在创建组合框时处理,该组合框从"select"元素读取选定的值.这样,您可以以编程方式设置默认值(就像您通常不使用自动完成组合框一样)

这是我添加的一行:

input.val( $("#combobox option:selected").text());
Run Code Online (Sandbox Code Playgroud)

干净利落.它将输入值设置为#combobox中所选元素的文本值.当然,您需要更新id元素以匹配您的单个项目或页面.

这是在上下文中:

(function($) {
    $.widget("ui.combobox", {
        _create: function() {
            var self = this;
            var select = this.element.hide();
            var input = $("<input>")
                .insertAfter(select)
                .autocomplete({
                    source: function(request, response) {
                        var matcher = new RegExp(request.term, "i");
                        response(select.children("option").map(function() {
                            var text = $(this).text();
                            if (this.value && (!request.term || matcher.test(text)))
                                return {
                                    id: this.value,
                                    label: text.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + $.ui.autocomplete.escapeRegex(request.term) + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>"),
                                    value: text
                                };
                        }));
                    },
                    delay: 0,
                    change: function(event, ui) {
                        if (!ui.item) {
                            // remove invalid value, as it didn't match anything
                            $(this).val("");
                            return false;
                        }
                        select.val(ui.item.id);
                        self._trigger("selected", event, {
                            item: select.find("[value='" + ui.item.id + "']")
                        });

                    },
                    minLength: 0
                })
                .addClass("ui-widget ui-widget-content ui-corner-left");



            // This line added to set default value of the combobox
            input.val( $("#combobox option:selected").text());





            $("<button>&nbsp;</button>")
            .attr("tabIndex", -1)
            .attr("title", "Show All Items")
            .insertAfter(input)
            .button({
                icons: {
                    primary: "ui-icon-triangle-1-s"
                },
                text: false
            }).removeClass("ui-corner-all")
            .addClass("ui-corner-right ui-button-icon")
            .click(function() {
                // close if already visible
                if (input.autocomplete("widget").is(":visible")) {
                    input.autocomplete("close");
                    return;
                }
                // pass empty string as value to search for, displaying all results
                input.autocomplete("search", "");
                input.focus();
            });
        }
    });

})(jQuery);
Run Code Online (Sandbox Code Playgroud)

  • 除了创建的无序列表中存在的默认值之外,还有什么方法可以使用默认值吗? (2认同)

edu*_*udi 19

根据Mathieu Steele的回答,而不是使用这个:

input.val( $("#combobox option:selected").text());
Run Code Online (Sandbox Code Playgroud)

我用这个:

input.val( $(select).find("option:selected").text());
Run Code Online (Sandbox Code Playgroud)

小部件现在可重复使用,干燥:)