使用 jquery 覆盖 javascript 确认

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21200688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:21:02  来源:igfitidea点击:

Overriding javascript confirm with jquery

javascriptjquery

提问by sandino

I want to override javascript confirm with jQuery dialog box. Here is my overridin code:

我想用 jQuery 对话框覆盖 javascript 确认。这是我的覆盖代码:

window.confirm = function(message, caption = 'Confirmation'){
    $(document.createElement('div')).attr({title: caption, 'class': 'dialog'}).html(message).dialog({
        position:['center',100],
        dialogClass: 'fixed',
        buttons: {
            "OK": function(){
                $(this).dialog('close');
                return true;
            },
            "Cancel": function(){
                $(this).dialog('close');
                return false;
            }
        },
        close: function(){
            $(this).remove();
        },
        draggable: false,
        modal: true,
        resizable: false,
        width: 'auto'
    });
};

And here is my action code:

这是我的操作代码:

if(confirm('Are you sure?') == false) { return false; }

This code does not work. How can I do this?

此代码不起作用。我怎样才能做到这一点?

回答by Arun P Johny

It is because the confirm method shows and dialog and it returns before the buttons are pressed.

这是因为确认方法显示和对话框并在按下按钮之前返回。

You can use a callback method to solve it

可以使用回调方法解决

window.confirm = function (message, callback, caption) {
    caption = caption || 'Confirmation'

    $(document.createElement('div')).attr({
        title: caption,
            'class': 'dialog'
    }).html(message).dialog({
        position: ['center', 100],
        dialogClass: 'fixed',
        buttons: {
            "OK": function () {
                $(this).dialog('close');
                callback()
                return true;
            },
                "Cancel": function () {
                $(this).dialog('close');
                return false;
            }
        },
        close: function () {
            $(this).remove();
        },
        draggable: false,
        modal: true,
        resizable: false,
        width: 'auto'
    });
};

confirm('dd', function () {
    //what every needed to be done on confirmation has to be done here
    console.log('confirmed')
})

Demo: Fiddle

演示:小提琴

You cannot use it with if..elsestatement

您不能将它与if..else语句一起使用

回答by sandino

I know is an old question but for completness I left my solution, is a plugin for jquery you only need to copy/paste this content save as .js file and include (along with jquery-ui) in your html and all alert and confirm dialog are replaced.

我知道这是一个老问题,但为了完整起见,我留下了我的解决方案,是一个 jquery 插件,您只需要复制/粘贴此内容另存为 .js 文件并在您的 html 中包含(与 jquery-ui 一起)和所有警报并确认对话框被替换。

I must point that above solution only calls the callback on user success (press OK button), but I wanted the callback to be called always, this way you can implement more behaviour

我必须指出,上述解决方案仅在用户成功时调用回调(按 OK 按钮),但我希望始终调用回调,这样您就可以实现更多行为

jQuery.cambiarAlert = function (options)
{
    var defaults = {
        title: "Atención",
        buttons: {
            "Aceptar": function()
            {
                jQuery(this).dialog("close");
            }
        }
    };

    jQuery.extend(defaults, options);

    delete defaults.autoOpen;


    window.alert = function ()
    {
        var html;

        try {
                html = arguments[0].replace(/\n/, "<br />")
            } catch (exception) {
                html = arguments[0]
        }

        jQuery("<div />", {
                            html: "<div class='.navbar-inverse .navbar-inner'>" + html + "</div>"
        }).dialog(defaults);
    };

    window.confirm = function (message, callback, caption) {
        caption = caption || 'Confirmación'

        $(document.createElement('div')).attr({
            title: caption,
            'class': 'dialog'
        }).html(message).dialog({
            buttons: {
                "Aceptar": function () {
                    $(this).dialog('close');
                    if (callback && typeof(callback) == "function"){
                        callback(true);
                    }
                    return true;
                },
                "Cancelar": function () {
                    $(this).dialog('close');
                    if (callback && typeof(callback) == "function"){
                        callback(false);
                    }
                    return false;
                }
            },
            close: function () {
                $(this).remove();
            },
            draggable: false,
            modal: true,
            resizable: false,
            width: 'auto'
        }).position({
           my: "center",
           at: "center",
           of: window
        });
    };

    return this;
};




$(function ()
{
    $.cambiarAlert();
});

The point here is that I do call callback(true) or callback(false) this way I can write a callback function that use the result as a parameter of the function itself

这里的重点是我确实调用 callback(true) 或 callback(false) 这样我可以编写一个回调函数,该函数使用结果作为函数本身的参数

So in my code I can do:

所以在我的代码中我可以这样做:

confirm("Are you sure?", function(result){
    if (result){
       //Perform ajax call
    }
})