简体   繁体   English

用jquery确认替换javascript确认

[英]Replace javascript confirm by jquery confirm

I use the following code to use default javascript confirm by jquery ui dialogue. 我使用以下代码来使用jquery ui对话的默认javascript确认。

jQuery.extend({
    confirm: function(message, title, okAction) {
        jQuery("<div></div>").dialog({
            // Remove the closing 'X' from the dialog
            open: function(event, ui) { jQuery(".ui-dialog-titlebar-close").hide(); }, 
            buttons: {
                "Ok": function() {
                    jQuery(this).dialog("close");
                    return true;
                },
                "Cancel": function() {
                    jQuery(this).dialog("close");
                    return false;
                }
            },
            close: function(event, ui) { jQuery(this).remove(); },
            resizable: false,
            title: title,
            modal: true
        }).text(message);
    }
});

jQuery.confirm(
        "LogOut",
        "Do you want to log out",
        function() { 
        });

Now I need to use this same code in a log out action. 现在我需要在注销操作中使用相同的代码。 So that I can replace the javascript confirm in the code. 这样我就可以在代码中替换javascript确认。

<a class="homeImage" onclick="return confirm('Do you want to logout?');" href="/future/myhome/head?$event=logout">LOG OUT</a>

The problem I am facing now is, when I replace the confirm with another function and wait for its return value to make the decision, the dialogue box doesn't return the value to a variable. 我现在面临的问题是,当我用另一个函数替换确认并等待其返回值来做出决定时,对话框不会将值返回给变量。 These two functions are executed simultaniously(its showing the alert, but it also get directed to the href target). 这两个函数同时执行(它显示警报,但它也被定向到href目标)。 Is there any way that the method can return a true or false value and hence proceed to the href target. 有没有办法让该方法返回true或false值,从而进入href目标。

reference: jQuery Extend , jQuery UI Replacement for alert 参考: jQuery ExtendjQuery UI替换警报
related question : js override confirm 相关问题: js覆盖确认

I don't know if you could actually do that as the jQuery.dialog function is asynchronous. 我不知道你是否真的可以这样做,因为jQuery.dialog函数是异步的。

You could use a promise library to setup the button click events. 您可以使用promise库来设置按钮单击事件。 But then you cannot simply specify a method in the onclick attribute and have to do it through code 但是,你不能简单地在onclick属性中指定一个方法,而必须通过代码来完成

var d = jQuery.Deferred();
d.resolve(true); // resolve is used then to mark the function as complete
return d.promise(); // return the promise

jsFiddle 的jsfiddle

jQuery.extend({
    confirm: function(message, title, okAction) {
        var d = jQuery.Deferred();
        jQuery("<div></div>").dialog({
            // Remove the closing 'X' from the dialog
            open: function(event, ui) { jQuery(".ui-dialog-titlebar-close").hide(); }, 
            buttons: {
                "Ok": function() {
                    jQuery(this).dialog("close");
                    d.resolve(true);
                    return true;
                },
                "Cancel": function() {
                    jQuery(this).dialog("close");
                    d.resolve(false);
                    return false;
                }
            },
            close: function(event, ui) { jQuery(this).remove(); },
            resizable: false,
            title: title,
            modal: true
        }).text(message);
        return d.promise();
    }
});

For more info about jQuery promise library see jQuery reference 有关jQuery promise库的更多信息,请参阅jQuery参考



Edit: Another way to to set it up: jsFiddle 编辑:另一种设置方式: jsFiddle

The problem is that default confirm dialog is synchronus and block the whole browser UI. 问题是默认confirm对话框是同步并阻止整个浏览器UI。 JQuery dialog is asynchronous and does not block UI (because it needs it to render). JQuery对话框是异步的,不会阻止UI(因为它需要渲染)。

So the answer to your problem is following. 所以问题的答案如下。 You need to change: 你需要改变:

         buttons: {
            "Ok": function() {
                window.location = "/future/myhome/head?$event=logout"
            },

and

 <a class="homeImage" onclick="return jQuery.confirm('Do you want to logout?');return false;" href="/future/myhome/head?$event=logout">LOG OUT</a>

Personally, I use confirm more for conditional execution of a function or posting a form... 就个人而言,我更多地使用确认函数的条件执行或发布表格...

So, using your example, I'd have made the following small changes: 所以,使用你的例子,我做了以下小改动:

buttons: {
         "Ok": function() {
              jQuery(this).dialog("close");
              okAction();  
         },

And then called the Confirm as follows: 然后称确认如下:

onclick="jQuery.confirm('Do you want to logout?','Confirm:',function(){window.location='/future/myhome/head?$event=logout'}); return false;">LOG OUT</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM