首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jConfirm警报- jQuery插件

jConfirm警报- jQuery插件
EN

Stack Overflow用户
提问于 2010-12-07 12:16:49
回答 1查看 33.1K关注 0票数 0

Am jConfirm用于用户确认。

我的第一个jConfirm不会因用户操作而停止,而只是传递到next。

我的代码:

代码语言:javascript
复制
    $(function () {

    $("#UpdateJobHandler").click(function () {

        var JobHander = getJobHandler();
        if (JobHander.MaxInstances == 0) {
                jConfirm('Continue?', 'Current Maximum Instances', function (ans) {
                    if (!ans)
                        return;
                });
        }

        var json = $.toJSON(JobHander);

        $.ajax({
            url: '../Metadata/JobHandlerUpdate',
            type: 'POST',
            dataType: 'json',
            data: json,
            contentType: 'application/json; charset=utf-8',
            success: function (data) {

                var message = data.Message;
                var alertM = data.MessageType;
                if (alertM == 'Error') {
                    $("#resultMessage").html(message);

                }

                if (alertM == 'Success') {
                    $("#resultMessage").empty();
                    alert(alertM + '-' + message);
                    action = "JobHandler";
                    controller = "MetaData";
                    loc = "../" + controller + "/" + action;
                    window.location = loc;
                }

                if (alertM == "Instances") {
                    jConfirm(message, 'Instances Confirmation?', function (answer) {
                        if (!answer)
                            return;
                        else {
                            var JobHandlerNew = getJobHandler();
                            JobHandlerNew.FinalUpdate = "Yes";
                            var json = $.toJSON(JobHandlerNew);
                            $.ajax({

                                url: '../Metadata/JobHandlerUpdate',
                                type: 'POST',
                                dataType: 'json',
                                data: json,
                                contentType: 'application/json; charset=utf-8',
                                success: function (data) {

                                    var message = data.Message;
                                    $("#resultMessage").empty();
                                    alert(alertM + '-' + message);
                                    action = "JobHandler";
                                    controller = "MetaData";
                                    loc = "../" + controller + "/" + action;
                                    window.location = loc;
                                }
                            });
                        }
                    });
                }
            }
        });
    });
});

我遗漏了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-12-07 12:28:21

不确定这是不是全部,但这一部分:

代码语言:javascript
复制
    if (JobHander.MaxInstances == 0) {
            jConfirm('Continue?', 'Current Maximum Instances', function (ans) {
                if (!ans)
                    return;
            });
    }

可能做不到你想要的。它正在退出function(ans) { ... }函数,而您可能想要退出整个处理程序,即$("#UpdateJobHandler").click(function () { ... }。如果是这样的话,你需要做类似于你下面做的事情--也就是把整个东西放在function(ans) { ... }中,在返回之后。可能最好的方法是分解成更小的函数。

编辑:以下内容:

代码语言:javascript
复制
    function afterContinue() {
       var json = $.toJSON(JobHander);

       $.ajax({
          // ... all other lines here ...
       });
    }

    if (JobHander.MaxInstances == 0) {
            jConfirm('Continue?', 'Current Maximum Instances', function (ans) {
                if (ans) {
                   afterContinue();
                }
            });
    }

您可以对所有success函数执行类似的操作。

另一个示例是,您可以像这样重写Instances检查:

代码语言:javascript
复制
            function afterInstances() {
                        var JobHandlerNew = getJobHandler();
                        JobHandlerNew.FinalUpdate = "Yes";

                        // ... and everything under else branch ...
            }

            if (alertM == "Instances") {
                jConfirm(message, 'Instances Confirmation?', function (answer) {
                    if (answer) {
                       afterInstances();
                    }
                });
            }

重要-重命名方法(afterContinueafterInstances,...)对于将来阅读这篇文章的人来说,有一个对他们有用的名字。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4373331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档