简体   繁体   English

自定义JavaScript确认对话框

[英]Custom javascript confirm dialog box

I have a function called showModalConfirmDialog that creates a custom javascript made dialog box with two buttons Yes/No and dims the background. 我有一个名为showModalConfirmDialog的函数,该函数使用两个按钮Yes / No创建一个自定义的javascript对话框,并使背景变暗。 Now in my functions I want to call that function like: 现在在我的函数中,我想像下面这样调用该函数:

var outcome = showModalConfirmDialog('Are you sure?');

and I want to react depending on the button clicked; 我想根据所单击的按钮做出反应;

if(outcome == true){
    // do something
} else {
    // do something else
}

The buttons return true/false. 这些按钮返回true / false。 Javascript code: JavaScript代码:

button1.onclick = function(evt){
    return true;
};

button2.onclick = function(evt){
    return false;
};

I don't know what I am missing, any help would be appreciated. 我不知道自己缺少什么,我们将不胜感激。 Thanks 谢谢

You can't reproduce the behaviour of the native modal. 您无法复制本机模式的行为。 Instead you could use callbacks. 相反,您可以使用回调。

This way : 这条路 :

function showModalConfirmDialog(msg, handler) {
    button1.onclick = function(evt){
        handler(true);
    };
    button2.onclick = function(evt){
        handler(false);
    };
}
showModalConfirmDialog('Are you sure?', function (outcome) { 
    alert(outcome ? 'yes' : 'no'); 
});

Or this way : 或者这样:

function showModalConfirmDialog(msg, confirmHandler, denyHandler) {
    button1.onclick = confirmHandler;
    button2.onclick = denyHandler;
}
showModalConfirmDialog(
    'Are you sure?', 
    function () { alert('yes'); }, 
    function () { alert('no'); }
);

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

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