使用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/
http://stackoverflow.com/a/42346781/4316802 –