Am jConfirm用于用户确认。
我的第一个jConfirm不会因用户操作而停止,而只是传递到next。
我的代码:
$(function () {
$("#UpdateJobHandler").click(function () {
var JobHander = getJobHandler();
if (JobHander.MaxInstances == 0) {
jConfirm('Continue?', 'Current Maximum Instances', function (ans) {
if (!ans)
return;
});
}
var json = $.toJSON(JobHander);
$.ajax({
url: '../Metadata/JobHandlerUpdate',
type: 'POST',
dataType: 'json',
data: json,
contentType: 'application/json; charset=utf-8',
success: function (data) {
var message = data.Message;
var alertM = data.MessageType;
if (alertM == 'Error') {
$("#resultMessage").html(message);
}
if (alertM == 'Success') {
$("#resultMessage").empty();
alert(alertM + '-' + message);
action = "JobHandler";
controller = "MetaData";
loc = "../" + controller + "/" + action;
window.location = loc;
}
if (alertM == "Instances") {
jConfirm(message, 'Instances Confirmation?', function (answer) {
if (!answer)
return;
else {
var JobHandlerNew = getJobHandler();
JobHandlerNew.FinalUpdate = "Yes";
var json = $.toJSON(JobHandlerNew);
$.ajax({
url: '../Metadata/JobHandlerUpdate',
type: 'POST',
dataType: 'json',
data: json,
contentType: 'application/json; charset=utf-8',
success: function (data) {
var message = data.Message;
$("#resultMessage").empty();
alert(alertM + '-' + message);
action = "JobHandler";
controller = "MetaData";
loc = "../" + controller + "/" + action;
window.location = loc;
}
});
}
});
}
}
});
});
});我遗漏了什么?
发布于 2010-12-07 12:28:21
不确定这是不是全部,但这一部分:
if (JobHander.MaxInstances == 0) {
jConfirm('Continue?', 'Current Maximum Instances', function (ans) {
if (!ans)
return;
});
}可能做不到你想要的。它正在退出function(ans) { ... }函数,而您可能想要退出整个处理程序,即$("#UpdateJobHandler").click(function () { ... }。如果是这样的话,你需要做类似于你下面做的事情--也就是把整个东西放在function(ans) { ... }中,在返回之后。可能最好的方法是分解成更小的函数。
编辑:以下内容:
function afterContinue() {
var json = $.toJSON(JobHander);
$.ajax({
// ... all other lines here ...
});
}
if (JobHander.MaxInstances == 0) {
jConfirm('Continue?', 'Current Maximum Instances', function (ans) {
if (ans) {
afterContinue();
}
});
}您可以对所有success函数执行类似的操作。
另一个示例是,您可以像这样重写Instances检查:
function afterInstances() {
var JobHandlerNew = getJobHandler();
JobHandlerNew.FinalUpdate = "Yes";
// ... and everything under else branch ...
}
if (alertM == "Instances") {
jConfirm(message, 'Instances Confirmation?', function (answer) {
if (answer) {
afterInstances();
}
});
}重要-重命名方法(afterContinue,afterInstances,...)对于将来阅读这篇文章的人来说,有一个对他们有用的名字。
https://stackoverflow.com/questions/4373331
复制相似问题