Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jquery重写javascript确认_Javascript_Jquery - Fatal编程技术网

使用jquery重写javascript确认

使用jquery重写javascript确认,javascript,jquery,Javascript,Jquery,我想用jQuery对话框覆盖javascript确认。这是我的重写代码: window.confirm = function(message, caption = 'Confirmation'){ $(document.createElement('div')).attr({title: caption, 'class': 'dialog'}).html(message).dialog({ position:['center',100], dialogCl

我想用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'
    });
};
这是我的行动代码:

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

此代码不起作用。如何执行此操作?

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

您可以使用回调方法来解决它

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')
})
演示:


您不能将其与
if..else
语句一起使用,因为确认方法显示和对话框,并在按下按钮之前返回

您可以使用回调方法来解决它

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')
})
演示:


您不能将其与
if..else
语句一起使用,我知道这是一个老问题,但为了完整性,我留下了我的解决方案,是jquery的插件,您只需复制/粘贴此内容另存为.js文件,并将其包括在html中(连同jquery ui),所有警报和确认对话框都将被替换

我必须指出,上述解决方案仅在用户成功时调用回调(按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();
});

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

我必须指出,上述解决方案仅在用户成功时调用回调(按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();
});

你说不工作是什么意思?它是否在控制台上抛出了一些js错误?要使其工作,函数
confirm()
应该只有在弹出窗口关闭后才能返回。从弹出窗口的按钮事件处理程序返回
true/false
,不会剪切它。它不会按预期工作,因为对话框是异步执行的。。。在您的情况下,确认不会返回任何内容(未定义)。。解决方法是使用回调方法。你说的“不工作”是什么意思?它是否在控制台上抛出了一些js错误?要使其工作,函数
confirm()
应该只有在弹出窗口关闭后才能返回。从弹出窗口的按钮事件处理程序返回
true/false
,不会剪切它。它不会按预期工作,因为对话框是异步执行的。。。在您的情况下,确认不会返回任何内容(未定义)。。解决方案是使用回调方法Uncaught TypeError:回调不是函数Uncaught TypeError:回调不是函数