【发布时间】:2020-08-18 03:11:06
【问题描述】:
以下是 HTML 代码。当我点击 Upload Building 时,会弹出以下文件选择模式。一旦用户选择一个文件并单击模式上的上传按钮,模式应该关闭,发送一个 AJAX 查询,然后用 HTML 响应替换上传按钮。
# HTML Code
<div class="container-fluid mx-auto">
<div class="row mainbox" id="input">
<div class="col-sm-5">
<button type="button" class="btn btn-secondary btn-sm btn-block" id="btn-upload-file" data-toggle="modal" data-target="#mdl-upload-file">Upload Building</button>
</div>
</div>
</div>
<div class="modal fade" id="mdl-upload-file" tabindex="-1" role="dialog" aria-labelledby="uploadFileModal" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="uploadFileModal">Upload Building Config</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="custom-file">
<input type="file" class="custom-file-input" id="customFile" accept="application/JSON">
<label class="custom-file-label">Choose file...</label>
<small id="uploadHelp" class="form-text text-muted">Please upload a valid building configuration JSON file.</small>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="btn-upload-selected-file">Upload File</button>
</div>
</div>
</div>
</div>
以下是执行此操作的 javascript 代码。
jQuery('#btn-upload-selected-file').on('click', function() {
$('#mdl-upload-file').modal('hide');
if (validBuildingFile) {
fileObj['build_type'] = 'upload';
jQuery.ajax({
type: "POST",
url: "/build",
data: JSON.stringify(fileObj),
dataType : "html",
contentType: "application/json",
success: function(response) {
jQuery('.mainbox').html(response);
},
error: function() {
alert(response);
}
});
}
});
现在,当我使用 alert 而不是 jQuery('.mainbox').html(response) 时,模式会成功关闭。但是,当我尝试显示响应时,模态的黑屏并没有消失。我尝试了针对堆栈溢出提供的多种解决方案(如下所示),但没有一个对我有用:
$('#mdl-upload-file').hide();$('#mdl-upload-file').remove();- 将超时设置为 5000
$('#modalElement').data('modal', null);
请帮忙!
【问题讨论】:
-
你需要调用 bootstrap 的模态隐藏函数。
$('#mdl-upload-file').modal("hide"); -
我在点击按钮时调用它,查看javascript代码
-
我试过
$('#modalElement').data('modal', null);,但没用。 -
这不是页面上唯一的解决方案...
标签: jquery twitter-bootstrap modal-dialog