繁体   English   中英

jQuery自动完成组合框:MVC3验证不起作用

[英]JQuery Autocomplete Combobox: MVC3 Validation not working

我正在尝试将JQuery UI自动完成(组合框)与ASP.NET MVC3中的下拉列表一起使用。

我认为这是代码的相关部分:

    <div class="editor-label">
        @Html.LabelFor(model => model.MerchantID, "Merchant")
    </div>
    <div class="editor-field">
        @Html.DropDownList("MerchantID", null, String.Empty, new { @class = "combobox" })
        @Html.ValidationMessageFor(model => model.MerchantID)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.FinancialAccountID, "FinancialAccount")
    </div>
    <div class="editor-field">
        @Html.DropDownList("FinancialAccountID", null, String.Empty, new { @class = "combobox" })
        @Html.ValidationMessageFor(model => model.FinancialAccountID)
    </div>

(从4-10个下拉列表中可以找到任何位置。)

这是javascript文件的内容,基本上是从jquery-ui网站复制的:

(function ($) {
    $.widget("ui.combobox", {
        _create: function () {
            var input,
                    self = this,
                    select = this.element.hide(),
                    selected = select.children(":selected"),
                    value = selected.val() ? selected.text() : "",
                    wrapper = this.wrapper = $("<span>")
                        .addClass("ui-combobox")
                        .insertAfter(select);

            input = $("<input>")
                    .appendTo(wrapper)
                    .val(value)
                    .addClass("ui-state-default ui-combobox-input")
                    .autocomplete({
                        delay: 0,
                        minLength: 0,
                        source: function (request, response) {
                            var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
                            response(select.children("option").map(function () {
                                var text = $(this).text();
                                if (this.value && (!request.term || matcher.test(text)))
                                    return {
                                        label: text.replace(
                                            new RegExp(
                                                "(?![^&;]+;)(?!<[^<>]*)(" +
                                                $.ui.autocomplete.escapeRegex(request.term) +
                                                ")(?![^<>]*>)(?![^&;]+;)", "gi"
                                            ), "<strong>$1</strong>"),
                                        value: text,
                                        option: this
                                    };
                            }));
                        },
                        select: function (event, ui) {
                            ui.item.option.selected = true;
                            self._trigger("selected", event, {
                                item: ui.item.option
                            });
                        },
                        change: function (event, ui) {
                            if (!ui.item) {
                                var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i"),
                                    valid = false;
                                select.children("option").each(function () {
                                    if ($(this).text().match(matcher)) {
                                        this.selected = valid = true;
                                        return false;
                                    }
                                });
                                if (!valid) {
                                    // remove invalid value, as it didn't match anything
                                    $(this).val("");
                                    select.val("");
                                    input.data("autocomplete").term = "";
                                    return false;
                                }
                            }
                        }
                    })
                    .addClass("ui-widget ui-widget-content ui-corner-left");

            input.data("autocomplete")._renderItem = function (ul, item) {
                return $("<li></li>")
                        .data("item.autocomplete", item)
                        .append("<a>" + item.label + "</a>")
                        .appendTo(ul);
            };

            $("<a>")
                    .attr("tabIndex", -1)
                    .attr("title", "Show All Items")
                    .appendTo(wrapper)
                    .button({
                        icons: {
                            primary: "ui-icon-triangle-1-s"
                        },
                        text: false
                    })
                    .removeClass("ui-corner-all")
                    .addClass("ui-corner-right ui-combobox-toggle")
                    .click(function () {
                        // close if already visible
                        if (input.autocomplete("widget").is(":visible")) {
                            input.autocomplete("close");
                            return;
                        }

                        // work around a bug (likely same cause as #5265)
                        $(this).blur();

                        // pass empty string as value to search for, displaying all results
                        input.autocomplete("search", "");
                        input.focus();
                    });
        },

        destroy: function () {
            this.wrapper.remove();
            this.element.show();
            $.Widget.prototype.destroy.call(this);
        }
    });
})(jQuery);

$(function () {
    $(".combobox").combobox();
});

我遇到的问题是似乎没有对输入的值进行任何验证。 例如,如果我将其保留为空白并提交,则不会出现表明输入无效信息的错误。 但是,如果我注释掉combobox()调用以禁用combobox功能,则验证会正常进行。

我已经在stackoverflow上进行了一些搜索,以解决此问题,但似乎找不到与该特定情况匹配的任何内容。

编辑:我发现验证确实确实发生了,但是只有当我在提交之前单击另一个字段时,这很奇怪。 因此,如果我在组合框中输入空白文本,然后单击一个文本字段,然后提交,则它可以正常工作(显示验证错误。)但是,如果我在组合框中输入空白文本并提交,则不会显示验证错误。 有任何想法吗?

好的,我知道了问题所在。

当按下提交按钮时,将在检测到错误输入时清除选定项目的自动完成JQuery 之前调用javascript验证。 这是我的意思部分:

if (!valid) {
     // remove invalid value, as it didn't match anything
     $(this).val("");
     select.val("");
     input.data("autocomplete").term = "";
     return false;
}

因此,我在用户开始键入内容后便立即清除了选定的项目,并且仅在选择了有效项目后才重新选择。

source: function (request, response) {
    select.val(""); //as soon as typing is started, the select input is cleared.  This makes sure validation always occurs
    var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
    response(select.children("option").map(function () {
        var text = $(this).text();
        if (this.value && (!request.term || matcher.test(text)))
            return {
                label: text.replace(
                    new RegExp(
                        "(?![^&;]+;)(?!<[^<>]*)(" +
                        $.ui.autocomplete.escapeRegex(request.term) +
                        ")(?![^<>]*>)(?![^&;]+;)", "gi"
                    ), "<strong>$1</strong>"),
                value: text,
                option: this
            };
    }));
},

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM