下面的代码是我对这个问题的解决方案。我记录了函数中的用法,但在此强调:
$.ConfirmDialog('Do you want to continue?', 'Continue Title', function() { alert('yes'); }, function() { alert('no'); }, null);
不需要特殊设置,只需在您的页面上包含“ConfirmDialog”代码块(我将我的放在我的 app.js 中)并使用上面的单行调用。享受!
$.ConfirmDialog = function (message, title, callbackYes, callbackNo, callbackArgument) {
/// <summary>
/// Generic confirmation dialog.
///
/// Usage:
/// $.ConfirmDialog('Do you want to continue?', 'Continue Title', function() { alert('yes'); }, function() { alert('no'); }, null);
/// $.ConfirmDialog('Do you want to continue?', 'Continue Title', function(arg) { alert('yes, ' + arg); }, function(arg) { alert('no, ' + arg); }, 'please');
///</summary>
///<param name="message" type="String">
/// The string message to display in the dialog.
///</param>
///<param name="title" type="String">
/// The string title to display in the top bar of the dialog.
///</param>
///<param name="callbackYes" type="Function">
/// The callback function when response is yes.
///</param>
///<param name="callbackNo" type="Function">
/// The callback function when response is no.
///</param>
///<param name="callbackNo" type="Object">
/// Optional parameter that is passed to either callback function.
///</param>
if ($("#modalConfirmDialog").length == 0)
$('body').append('<div id="modalConfirmDialog"></div>');
var dlg = $("#modalConfirmDialog")
.html(message)
.dialog({
autoOpen: false, // set this to false so we can manually open it
dialogClass: "confirmScreenWindow",
closeOnEscape: true,
draggable: false,
width: 460,
minHeight: 50,
modal: true,
resizable: false,
title: title,
buttons: {
Yes: function () {
if (callbackYes && typeof (callbackYes) === "function") {
if (callbackArgument == null) {
callbackYes();
} else {
callbackYes(callbackArgument);
}
}
$(this).dialog("close");
},
No: function () {
if (callbackNo && typeof (callbackNo) === "function") {
if (callbackArgument == null) {
callbackNo();
} else {
callbackNo(callbackArgument);
}
}
$(this).dialog("close");
}
}
});
dlg.dialog("open");
};