【问题标题】:Confirmation with save and discard button使用保存和丢弃按钮进行确认
【发布时间】:2011-05-31 10:58:12
【问题描述】:
我们如何在 javascript 中创建一个带有保存和丢弃按钮的确认警报?
如果我们使用代码
confirm('Do you want to save it?');
我们将收到一个带有确定取消的警报框。
我们怎样才能让ok按钮的文本作为save而另一个作为discard呢?
【问题讨论】:
标签:
javascript
confirmation
【解决方案1】:
您不能修改默认的 javascript 方法“确认”。但是,您可以覆盖它,例如,使用 jQuery UI 对话框:
window.confirm = function (message) {
var html = "<div style='margin:20px;'><img style='float:left;margin-right:20px;' src='/img/confirm.gif' alt='Confirm'/><div style='display:table;height:1%;'>" + message + "</div></div>";
$(html).dialog({ closeOnEscape: false,
open: function (event, ui) { $('.ui-dialog-titlebar-close').hide(); },
modal: true,
resizable: false,
width: 400,
title: "Confirmation",
buttons: {
"Save": function () {
//Do what you need
},
"Cancel": function () {
$(this).dialog("close");
}
}
});
}
【解决方案3】:
- 连接一些tons JS框架。比如jQuery+UI
- 覆盖 window.confirm 方法,将其作为您最喜欢的 JS UI 框架的包装器。
- 利润!!!