我正在使用 ASP.NET MVC 4
、jQuery
和 jQuery UI
。
我的 View 中有一个对话框。当我单击按钮时,会弹出对话框,获取对话框中的值并将其发送到服务。该服务执行其需要执行的操作,如果成功,将发回一条空白消息,否则将发回实际的错误消息。之后,我需要检查客户端的错误,关闭当前对话框并打开成功对话框或错误对话框。我不确定如何关闭当前对话框并显示另一个对话框。
我的按钮:
<button id="TestButton" type="button">Display pop up</button>
我的对话框:
<div id="confirmationDialog"></div>
<div id="successDialog"></div>
<div id="errorDialog">error dialog</div>
我的 jQuery 代码:
$('#TestButton').click(function () {
$('#confirmationDialog').dialog('open');
});
$('#errorDialog').dialog({
autoOpen: false,
modal: true,
resizable: false,
width: 500,
title: 'Add Rule Detail Error',
buttons: {
'Ok': function () {
$(this).dialog('close');
}
}
});
$('#confirmationDialog').dialog({
autoOpen: false,
modal: true,
resizable: false,
width: 330,
title: 'Add Rule Detail Confirmation',
open: function (event, ui) {
$(this).load('@Url.Action("AddRuleConfirmation")' +
'?systemCode=' + $('#SystemCode').val());
},
buttons: {
'Yes': function () {
var url = '@Url.Action("AddRuleConfirmationSubmit")';
var data = {
systemCode: $('#SystemCode').val()
};
$.getJSON(url, data, function (message) {
alert(message);
if (message == '') {
$(this).dialog('close');
}
else {
$(this).dialog('close');
$('#errorDialog').dialog('open');
}
});
},
'No': function () {
$(this).dialog('close');
}
}
});
我的行动方法:
public ActionResult AddRuleConfirmation(string systemCode)
{
DetailConfirmationViewModel viewModel = new DetailConfirmationViewModel()
{
SystemCode = systemCode
};
return PartialView("_AddRuleConfirmation", viewModel);
}
public ActionResult AddRuleConfirmationSubmit(string systemCode)
{
CreateRuleViewModel viewModel = new CreateRuleViewModel()
{
SystemCode = systemCode
};
ResCodeRuleAdd_Type result = ruleService.AddRule(viewModel);
string message = string.Empty;
if (result != ResCodeRuleAdd_Type.R00)
{
// Get the error message from resource file
message = ...
}
return Json(message, JsonRequestBehavior.AllowGet);
}
如何在 get JSON 调用后关闭当前弹出窗口并打开另一个弹出窗口?
最佳答案
您必须首先将对话框添加到页面:将其放在当前的之前:
$('#errorDialog').dialog({
autoOpen: false,
modal: true,
resizable: false,
width: 330,
title: 'My Error Dialog'
});
//current code follows:
$('#confirmationDialog').dialog({
那么你所拥有的应该可以工作。
编辑:我想了一下,您可能需要修复成功处理程序内 $(this)
的范围。
更改要做的事情:
var myDialog = $('#confirmationDialog').dialog({
然后使用:
myDialog.dialog('close');
在该处理程序中关闭第一个对话框。
关于javascript - 如何在 AJAX JSON 调用后关闭 jQuery 对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16085422/