返回AJAX响应后,防止默认操作

时间:2017-06-21 09:14:57

标签: javascript jquery asp.net ajax sweetalert

在调用GetCurrentTime的AJAX并返回响应对象之后,被调用的新甜蜜警报仅保留一秒,因为防止默认不会阻止事件运行。如何阻止执行此操作,以便弹出窗口一直保持到单击状态为止。初始event.preventDefault()与单击按钮时的工作方式相同。

 function alertYourself(ctl, event) {
        var defaultAction = $(ctl).prop("href");
        event.preventDefault();
        swal({
            title: 'SUBMIT DATA',
            input: 'email',
            showCancelButton: true,
            confirmButtonText: 'Submit',
            },
            function (isConfirm) {
                if (isConfirm) {
                    event.preventDefault();
                    $.ajax({
                        type: "POST",
                        url: "Home.aspx/GetCurrentTime",
                        data: '{name: "ITS ALIVE" }',
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {
                            event.preventDefault();
                            swal({
                                title: "Are you sure?",
                                text: response.d,
                                type: "warning",
                                showCancelButton: true,
                                confirmButtonText: "Continue",
                                cancelButtonText: "Cancel",
                                closeOnConfirm: false,
                                closeOnCancel: true
                            },
                             function (isConfirm) {
                                 if (isConfirm) {
                                     window.location.href = defaultAction;
                                     return true;
                                 } else {
                                     return false;
                                 }
                             });
                        },
                        error: function (response) {
                            this.event.preventDefault();
                        }
                    });
                } else {
                }
            });
    }

0 个答案:

没有答案