1

我有一个名为的函数showModalConfirmDialog,它创建一个带有两个按钮是/否的自定义 javascript 对话框,并使背景变暗。现在在我的函数中,我想调用该函数,例如:

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

我想根据点击的按钮做出反应;

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

按钮返回真/假。Javascript代码:

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

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

我不知道我错过了什么,任何帮助将不胜感激。谢谢

4

1 回答 1

5

您无法重现本机模式的行为。相反,您可以使用回调。

这边走 :

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'); 
});

或者这样:

function showModalConfirmDialog(msg, confirmHandler, denyHandler) {
    button1.onclick = confirmHandler;
    button2.onclick = denyHandler;
}
showModalConfirmDialog(
    'Are you sure?', 
    function () { alert('yes'); }, 
    function () { alert('no'); }
);
于 2013-11-14T13:05:16.523 回答