你知道为什么为我的下拉框引入 jquery ui 自动完成功能也会改变我的列表框吗?

发布于 2024-08-27 12:46:14 字数 3594 浏览 4 评论 0原文

我正在尝试更改组合框以使用自动完成功能,因此我利用此处列出的代码(其中非常适合我的下拉菜单)

问题是我也在同一页面上有一个包含以下代码的列表框:

<%= Html.ListBox("Cars", Model.BodyParts.Select(
                    x => new SelectListItem {
                        Text = x.Name,
                        Value = x.Id,
                        Selected = Model.CarsSelected.Any(y => y.Id == x.Id)
                    }
               ))%>

并且 jquery ui 代码似乎也将其更改为自动完成下拉列表(而不是将其保留为多菜单)选择列表框)

知道如何防止这种情况发生吗?

我实际上只是使用此页面上的确切代码

<script type="text/javascript">
(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 (!request.term || matcher.test(text))
                                return {
                                    id: $(this).val(),
                                    label: text.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + request.term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>"),
                                    value: text
                                };
                        }));
                    },
                    delay: 0,
                    select: function(e, ui) {
                        if (!ui.item) {
                            // remove invalid value, as it didn't match anything
                            $(this).val("");
                            return false;
                        }
                        $(this).focus();
                        select.val(ui.item.id);
                        self._trigger("selected", null, {
                            item: select.find("[value='" + ui.item.id + "']")
                        });

                    },
                    minLength: 0
                })
                .addClass("ui-widget ui-widget-content ui-corner-left");
            $("<button>&nbsp;</button>")
            .insertAfter(input)
            .button({
                icons: {
                    primary: "ui-icon-triangle-1-s"
                },
                text: false
            }).removeClass("ui-corner-all")
            .addClass("ui-corner-right ui-button-icon")
            .position({
                my: "left center",
                at: "right center",
                of: input,
                offset: "-1 0"
            }).css("top", "")
            .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);

$(function() {
    $("select").combobox();
});
</script>

I am trying to change my comboboxes to use autocomplete so i leverage the code listed here (which worked perfectly for my dropdowns)

The issue is that i also on the same page have a listbox with the following code:

<%= Html.ListBox("Cars", Model.BodyParts.Select(
                    x => new SelectListItem {
                        Text = x.Name,
                        Value = x.Id,
                        Selected = Model.CarsSelected.Any(y => y.Id == x.Id)
                    }
               ))%>

and it appears that the jquery ui code is changing this to a autocomplete dropdown as well (as opposed to keeping it as a multi select list box)

any idea how to prevent this from happening?

i literally am just using the exact code on this page

<script type="text/javascript">
(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 (!request.term || matcher.test(text))
                                return {
                                    id: $(this).val(),
                                    label: text.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + request.term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>"),
                                    value: text
                                };
                        }));
                    },
                    delay: 0,
                    select: function(e, ui) {
                        if (!ui.item) {
                            // remove invalid value, as it didn't match anything
                            $(this).val("");
                            return false;
                        }
                        $(this).focus();
                        select.val(ui.item.id);
                        self._trigger("selected", null, {
                            item: select.find("[value='" + ui.item.id + "']")
                        });

                    },
                    minLength: 0
                })
                .addClass("ui-widget ui-widget-content ui-corner-left");
            $("<button> </button>")
            .insertAfter(input)
            .button({
                icons: {
                    primary: "ui-icon-triangle-1-s"
                },
                text: false
            }).removeClass("ui-corner-all")
            .addClass("ui-corner-right ui-button-icon")
            .position({
                my: "left center",
                at: "right center",
                of: input,
                offset: "-1 0"
            }).css("top", "")
            .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);

$(function() {
    $("select").combobox();
});
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独守阴晴ぅ圆缺 2024-09-03 12:46:14

我猜测您的 JQuery 选择器(您的问题中未提供)正在抓取页面上的所有下拉菜单。您应该考虑使其更具体,最有可能通过 ID 引用元素。 JQuery 文档中有很多很好的示例:

http://api.jquery.com/category/选择器/

I'm guessing that your JQuery selector (not provided in your question) is grabbing all dropdowns on the page. You should look at making it more specific, most likely referencing the element by ID. There are lots of good examples in the JQuery docs:

http://api.jquery.com/category/selectors/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文