我有一个函数showModalConfirmDialog
,它使用两个按钮“是/否”创建一个自定义的javascript对话框,并使背景变暗。现在在我的函数中,我想像下面这样调用该函数:
var outcome = showModalConfirmDialog('Are you sure?');
我想根据所单击的按钮做出反应;
if(outcome == true){
// do something
} else {
// do something else
}
这些按钮返回true / false。JavaScript代码:
button1.onclick = function(evt){
return true;
};
button2.onclick = function(evt){
return false;
};
我不知道自己缺少什么,任何帮助将不胜感激。谢谢
您无法重现本机模式的行为。相反,您可以使用回调。
这条路 :
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'); }
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句