使用 id="confirmation" 在 HTML 中创建模态对话框并使用函数 showConfirmation。
还请记住,您应该在隐藏模式对话框后取消绑定 (modal.unbind()) 取消和成功按钮。如果你不这样做,你会得到双重绑定。
例如:如果您打开一次对话框并按“取消”,然后第二次打开对话框并按“确定”,您将执行 2 次成功回调。
showConfirmation = function(title, message, success, cancel) {
title = title ? title : 'Are you sure?';
var modal = $("#confirmation");
modal.find(".modal-title").html(title).end()
.find(".modal-body").html(message).end()
.modal({ backdrop: 'static', keyboard: false })
.on('hidden.bs.modal', function () {
modal.unbind();
});
if (success) {
modal.one('click', '.modal-footer .btn-primary', success);
}
if (cancel) {
modal.one('click', '.modal-header .close, .modal-footer .btn-default', cancel);
}
};
// bind confirmation dialog on delete buttons
$(document).on("click", ".delete-event, .delete-all-event", function(event){
event.preventDefault();
var self = $(this);
var url = $(this).data('url');
var success = function(){
alert('window.location.href=url');
}
var cancel = function(){
alert('Cancel');
};
if (self.data('confirmation')) {
var title = self.data('confirmation-title') ? self.data('confirmation-title') : undefined;
var message = self.data('confirmation');
showConfirmation(title, message, success, cancel);
} else {
success();
}
});
https://jsfiddle.net/yiiBoy/hne9sp6g/