你需要做的是使用 jQuery.deferred/promise。
http://api.jquery.com/deferred.promise/ http://api.jquery.com/deferred.promise/
在此示例中,asyncEvent
1)创建一个jquery延迟对象
2)有解决/拒绝的逻辑,你的确定/取消
3)返回一个 deferred.promise() 对象,然后可以将其与 $.when 一起使用来确定延迟对象是被解析还是被拒绝(确定/取消)。
你会做的是
1)创建一个jquery延迟对象
2)启动对话框,使用确定/取消设置 deferred.resolve/reject
3)返回一个 deferred.promise() 对象,
4)将延迟的 Promise 对象与 $.when 一起使用http://api.jquery.com/jQuery.when/ http://api.jquery.com/jQuery.when/
function customConfirm(customMessage) {
var dfd = new jQuery.Deferred();
$("#popUp").html(customMessage);
$("#popUp").dialog({
resizable: false,
height: 240,
modal: true,
buttons: {
"OK": function () {
$(this).dialog("close");
alert(true);
dfd.resolve();
},
Cancel: function () {
$(this).dialog("close");
alert(false);
dfd.reject();
}
}
});
return dfd.promise();
}
$.when( customConfirm('hey') ).then(
function() {
alert( "things are going well" );
},
function( ) {
alert( "you fail this time" );
});
您也可以只使用解析并确定 $.when 中的确认是真还是假,
function customConfirm(customMessage) {
var dfd = new jQuery.Deferred();
$("#popUp").html(customMessage);
$("#popUp").dialog({
resizable: false,
height: 240,
modal: true,
buttons: {
"OK": function () {
$(this).dialog("close");
alert(true);
dfd.resolve(true);
},
Cancel: function () {
$(this).dialog("close");
alert(false);
dfd.resolve(false);
}
}
});
return dfd.promise();
}
$.when( customConfirm('hey') ).then(
function(confirm) {
if(confirm){alert( "things are going well" );}
else{alert( "you fail this time" );}
});
希望有帮助。