JQuery 确认对话框

标签 jquery jquery-ui confirm-dialog

我正在寻找一种方法来使用 JQuery 实现可重用的“确认”对话框。

这是 MyApp 类中用于打开对话框的部分:

/**
 * @param text string Message to display
 */
getConfirmationDialog: function(text) {
   MyApp.confirmDialog = $('<div><p>' + text + '</p></div>');
   MyApp.confirmDialog
    .dialog({
        modal: true,
        autoOpen: false,
        title: 'Please confirm',
        width: 300,
        height: 180,
        buttons: {
            'OK': function() {
                return true;
            },
            Cancel: function() {
                $(this).dialog('close');
                return false;
            }
        }
    });

    MyApp.confirmDialog.dialog('open');
},

在另一个类中我这样做:

/**
 * Clear system cache
 *
 * @param url string Backend URL
 */
clearCache: function(url) {

    dialog = MyApp.getConfirmationDialog('Clear cache?');

    //dialog returns true..
    if (dialog) {
        MyApp.admin.dashboard.doClearCache();
    }

},

但我不知道以“正确”的方式执行此操作..对话框无法返回值或?

最佳答案

下面的代码是我解决这个问题的方法。我记录了该函数的用法,但在这里强调一下:

$.ConfirmDialog('Do you want to continue?', 'Continue Title', function() { alert('yes'); }, function() { alert('no'); }, null);

无需特殊设置,只需在页面上包含“ConfirmDialog”代码块(我将其放在 app.js 中)并使用上面的单行进行调用。享受!

$.ConfirmDialog = function (message, title, callbackYes, callbackNo, callbackArgument) {
    /// <summary>
    ///     Generic confirmation dialog.
    ///
    ///     Usage:
    ///         $.ConfirmDialog('Do you want to continue?', 'Continue Title', function() { alert('yes'); }, function() { alert('no'); }, null);
    ///         $.ConfirmDialog('Do you want to continue?', 'Continue Title', function(arg) { alert('yes, ' + arg); }, function(arg) { alert('no, ' + arg); }, 'please');
    ///</summary>
    ///<param name="message" type="String">
    ///     The string message to display in the dialog.
    ///</param>
    ///<param name="title" type="String">
    ///     The string title to display in the top bar of the dialog.
    ///</param>
    ///<param name="callbackYes" type="Function">
    ///     The callback function when response is yes.
    ///</param>
    ///<param name="callbackNo" type="Function">
    ///     The callback function when response is no.
    ///</param>
    ///<param name="callbackNo" type="Object">
    ///     Optional parameter that is passed to either callback function.
    ///</param>

    if ($("#modalConfirmDialog").length == 0)
        $('body').append('<div id="modalConfirmDialog"></div>');

    var dlg = $("#modalConfirmDialog")
        .html(message)
        .dialog({
            autoOpen: false, // set this to false so we can manually open it
            dialogClass: "confirmScreenWindow",
            closeOnEscape: true,
            draggable: false,
            width: 460,
            minHeight: 50,
            modal: true,
            resizable: false,
            title: title,
            buttons: {
                Yes: function () {
                    if (callbackYes && typeof (callbackYes) === "function") {
                        if (callbackArgument == null) {
                            callbackYes();
                        } else {
                            callbackYes(callbackArgument);
                        }
                    }

                    $(this).dialog("close");
                },

                No: function () {
                    if (callbackNo && typeof (callbackNo) === "function") {
                        if (callbackArgument == null) {
                            callbackNo();
                        } else {
                            callbackNo(callbackArgument);
                        }
                    }

                    $(this).dialog("close");
                }
            }
        });
    dlg.dialog("open");
};

关于JQuery 确认对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4422486/

相关文章:

jquery - Typescript Promise with JQuery 似乎运行了两次?

javascript - 检查数组中的值是否在范围内

javascript - 确认对话框在 iOS10 上失去焦点

java - 我可以在 java 中的消息对话框中使用 IF 语句吗?

javascript - 如何使用带有 Backbone.js 库的 jQuery 添加事件类

javascript - jquery/javascript 错误 - 太多递归 - 使用自调用函数

php - 如何使用 jQuery 初始化 href

jquery - 可使用单独的 CSS3 缩放的连接容器进行排序,无法正常工作以移动到以下容器

javascript - jQueryUI 自动完成默认文本

javascript - 我怎样才能有一个带有标题的确认对话框,在 "OK"和 "Cancel"事件上返回 true/false?