j确认警报 – jQuery插件
我j确认用户确认。
我的第一个jConfirm不会停止用户操作,只是转到下一个。
我的代码:
$(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; } }); } }); } } }); }); });
我错过了什么?
不确定这是否全部,但这一部分:
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
function执行类似的操作。
另一个例子,您可以像这样重写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
,…)以获得一些名称,这意味着对将来阅读此内容的人有用。