jConfirm 警报 - jQuery 插件 [英] jConfirm alert - jQuery plugin

查看:25
本文介绍了jConfirm 警报 - jQuery 插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Am jConfirm 用于用户确认.

Am jConfirm for user confirmation.

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

My first jConfirm doesnt stop for user action and just passes to next.

我的代码:

    $(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;
                                }
                            });
                        }
                    });
                }
            }
        });
    });
});

我错过了什么?

推荐答案

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

Not sure if this is all, but this part:

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

可能不会做你想做的事.它正在退出 function(ans) { ... } 函数,而您可能想要退出整个处理程序,即 $("#UpdateJobHandler").click(function () {... }.如果是这样,您需要执行类似于下面的操作 - 即将整个内容放在 function(ans) { ... } 中,在返回之后. 可能最好分成更小的函数.

probably doesn't do what you want. It is exiting the function(ans) { ... } function, while you probably want to exit the whole handler, i.e. $("#UpdateJobHandler").click(function () { ... }. If so, you would need to do similar to what you do below - i.e. put the whole thing in function(ans) { ... }, after the return. Probably best to separate into smaller functions.

沿着这些思路:

    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 函数执行类似的操作.

You can do similar thing for all the success functions.

另一个例子,你可以像这样重写 Instances 检查:

Another example, you can rewrite the Instances check like this:

            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、...),使其具有对将来阅读本文的人有用的名称.

Important - rename the methods (afterContinue, afterInstances, ...) to have some name that means something useful to someone reading this in the future.

这篇关于jConfirm 警报 - jQuery 插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆