0

嗨,我有一些 jquery 代码检查 asp 验证器是否有 style="display:inline" 如果有,我将错误类添加到作为 div 的错误父级。

但是当我有两个验证器时,这不起作用。这是为什么?这是代码

////////// Add Error Class  ////////////////

addErrorClass: function (obj) {


    if ($(obj).parent().find('.errorMessage').parent().css("display") == "inline") {

        $(obj).parent().addClass("error");

    } else {
        $(obj).parent().removeClass("error");
    }


},

////////// Add Error Class /////////////

////////// Validate Form Fields /////////////


ValidateFF: function () {



    $('.required input').blur(function () {


        project.addErrorClass(this);
        if ($(".modal").length) {
            Spacedadi_UI.AdjustRadWindow();
        };

    });


    $('#btnSave').click(function () {

        $('.required input').each(function (index) {

            // Force ASP Validation before click event when using link buttons

            var cErrorID = $(this).parent().find("span").attr('id');
            ValidatorEnable(document.getElementById(cErrorID), true);

            project.addErrorClass(this);

            if ($(".modal").length) {
                project.AdjustRadWindow();
            };
        }); //end of click

        if ($(".modal").length) {
            project.AdjustRadWindow();
        };

    }); // end validate function

},
////////// Validate Form Fields /////////////

HTML 代码

                <div class="field required"><asp:Label ID="lbEmail" runat="server" CssClass="label" Text="Email" AssociatedControlID="tbEmail"></asp:Label>
            <asp:TextBox ID="tbEmail" runat="server"></asp:TextBox>

            <asp:RequiredFieldValidator ID="rfvEmail" runat="server" errorMessage="<span class='errorMessage'>Please enter an email address</span>"
                ControlToValidate="tbEmail" CssClass="" ValidationGroup="ValAddContact"
                Display="Dynamic"></asp:RequiredFieldValidator>

            <asp:RegularExpressionValidator ID="revtbEmail" runat="server" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
                ControlToValidate="tbEmail" errorMessage="<span class='errorMessage'>Invalid Email Format</span>" ValidationGroup="ValAddContact"
                CssClass=""></asp:RegularExpressionValidator>
        </div>

谢谢

4

1 回答 1

1

I'm not really sure about ASP.NET, but have you tried inspecting the HTML on the page to see if it's renamed anything you're relying on? I seem to remember ASP renaming IDs and Names of elements to ensure they're unique.

You could also try using .hasClass('errorMessage') instead of find(...).

Does the button click event actually fire in the first place?

于 2012-12-13T10:07:51.123 回答