0

我有这个脚本:

var pUserName = $('#UserName'), pPassword = $('#Password'), pRememberMe = $('#RememberMe');

$(function () {
    Initialize();

    $('#logon button').click(function () {
        $.event.trigger('AjaxButtonClick', this);
        $.event.trigger('AjaxLoader', '#logon .form');
        $('form#logon').valid();
    });
});

function Initialize() {
    returnUrl = getParameterByName('ReturnUrl');

    $('form#logon').validate({
        submitHandler: function (form) {
            logOn(returnUrl);
        },
        invalidHandler: function (e, validator) {
            showAnimatedStatusText($('.message'), 'Please fill in the required fields highlighted below.', 'warning');
            $.event.trigger('AjaxButtonError');
            $.event.trigger('AjaxLoader');
        },
        errorPlacement: function (error, element) {
            return true;
        },
        highlight: function (element) {
            $(element).parent().addClass("input-validation-error");
        },
        unhighlight: function (element) {
            $(element).parent().removeClass("input-validation-error");
        }
    });
};

function logOn(returnUrl) {
    $.ajax({
        url: '/_Account/LogOn',
        type: 'POST',
        data: '{ "UserName": "' + pUserName.val() + '", "Password": "' + pPassword.val() + '", "RememberMe": ' + pRememberMe.is(':checked') + ', "returnUrl": "' + returnUrl + '" }',
        dataType: 'text json',
        contentType: 'application/json; charset=utf-8',
        success: function (o) {
            if (o.success) {
                // Success code

                $.event.trigger('AjaxButtonSuccess');
            } else {
                showAnimatedStatusText($('.message'), o.error, 'error');
                $.event.trigger('AjaxButtonError');
            }

            $.event.trigger('AjaxLoader');
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            showAnimatedStatusText($('.message'), 'An uncaught error has occurred, please contact your system administrator (Error code: 150).', 'error');
            $.event.trigger('AjaxButtonError');
            $.event.trigger('AjaxLoader');
        }
    });
};

这在除 ie8 之外的所有浏览器中都可以正常工作。出于某种原因,从不调用 submitHandler。如果我输入不正确的数据,invalidHandler 会被调用,但 submitHandler 永远不会。

有谁知道为什么?

干杯,/r3plica

4

1 回答 1

-1

问题是在 ie8 中使用元素的工作方式似乎与 an 不同,即使您提供 . 为了使它工作,我必须将我的验证脚本更改为如下所示:

    $('#logon').validate({
        submitHandler: function () {
            // Do not submit
        },
        invalidHandler: function (e, validator) {
            showAnimatedStatusText($('.message'), 'Please fill in the required fields highlighted below.', 'warning');
            $.event.trigger('AjaxButtonError');
            $.event.trigger('AjaxLoader');
        },
        errorPlacement: function (error, element) {
            return true;
        },
        highlight: function (element) {
            $(element).parent().addClass("input-validation-error");
        },
        unhighlight: function (element) {
            $(element).parent().removeClass("input-validation-error");
        }
    });

调用按钮单击如下所示:

    $('#logon button').click(function () {
        $.event.trigger('AjaxButtonClick', this);
        $.event.trigger('AjaxLoader', '#logon .form');
        if ($('#logon').valid()) {
            logOn(returnUrl);
        };
    });

这样做解决了这个问题。

于 2013-05-23T12:20:06.487 回答