[英]Bootbox dialog.modal('hide') doesn't hide modal
我有 bootbox.js 模态的问题。 我想使用 bootbox.dialog 在执行 Ajax 并等待响应时暂停 UI。 如果我使用dialog.modal('hide');
一切都很酷dialog.modal('hide');
在 bootbox.alert 中使用确认(单击确定后它会隐藏 dialog.modal),但我不想每次都确认它。 当我使用dialog.modal('hide');
在 bootbox.alert 之外确认它不会隐藏 dialog.modal ... 问题出在哪里? 工作代码(隐藏模式)在success
,而不工作在error
var dialog = bootbox.dialog({
message: '<p class="text-center mb-0"><i class="fa fa-spin fa-cog"></i> Please wait while we do something...</p>',
closeButton: false
});
var checkboxId = "#" + $(this).attr('id');
var checked = $(this).is(":checked");
if (checked) {
$.ajax({
url: url,
type: "POST",
data: { estimatedCostId: @Model.EstimatedCostID },
success: function (data) {
if (!data.Success)
{
bootbox.alert(data.ErrorMessage, function () {
dialog.modal('hide');
});
$(checkboxId).prop('checked', !checked);
}
},
error: function (request, status, error) {
dialog.modal('hide');
bootbox.alert("Błąd serwera");
$(checkboxId).prop('checked', !checked);
}
});
}
最有可能的是,这是一个时间问题。 如果您的 AJAX 调用“太快”,则在 bootbox.dialog 函数解析之前调用成功/失败回调。 所以这:
dialog.modal('hide');
最终在未定义的对象上被调用。 我在最近的一个项目中遇到了类似的问题,并通过将 AJAX 调用放入show.bs.modal事件中来解决它,如下所示:
var checkboxId = "#" + $(this).attr('id');
var checked = $(this).is(":checked");
var dialog = bootbox.dialog({
message: '<p class="text-center mb-0"><i class="fa fa-spin fa-cog"></i> Please wait while we do something...</p>',
closeButton: false
})
// this is the change
.on('shown.bs.modal', function(){
if (checked) {
$.ajax({
url: url,
type: "POST",
data: { estimatedCostId: @Model.EstimatedCostID },
success: function (data) {
if (!data.Success)
{
bootbox.alert(data.ErrorMessage, function () {
dialog.modal('hide');
});
$(checkboxId).prop('checked', !checked);
}
},
error: function (request, status, error) {
dialog.modal('hide');
bootbox.alert("Błąd serwera");
$(checkboxId).prop('checked', !checked);
}
});
}
});
您也可以以简单的方式使用它
bootbox.hideAll();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.