模态屏幕在模态隐藏后显示 AJAX 响应时保持黑色
The modal screen remains black on showing AJAX response after modal hide
以下是HTML代码。当我点击上传建筑物时,会弹出以下文件选择模式。一旦用户选择一个文件并点击模态框上的上传按钮,模态框应该关闭,发送一个 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);
}
});
}
});
现在,当我使用警报而不是 jQuery('.mainbox').html(response)
时,模态成功关闭。但是,当我尝试显示响应时,模态框的黑屏并没有消失。我尝试了堆栈溢出时提供的多种解决方案(如下所示),但其中 none 对我有用:
$('#mdl-upload-file').hide();
$('#mdl-upload-file').remove();
- 将超时设置为 5000
$('#modalElement').data('modal', null);
请帮忙!
只需将 data-dismiss
添加到您的 [Upload File]
按钮即可:
<button type="button" data-dismiss="modal" disabled class="btn btn-primary" id="btn-upload-selected-file">Upload File</button>
然后去掉多余的$('#mdl-upload-file').modal('hide')
。这是不好的做法。相反,您可以阻止用户在
选择文件之前单击 [Upload File]
$('#customFile').on('change',function(){
if (this.files.length>0) $('#btn-upload-selected-file').removeAttr('disabled')
})
也已将 disabled
属性添加到 [Upload File]
。
以下是HTML代码。当我点击上传建筑物时,会弹出以下文件选择模式。一旦用户选择一个文件并点击模态框上的上传按钮,模态框应该关闭,发送一个 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);
}
});
}
});
现在,当我使用警报而不是 jQuery('.mainbox').html(response)
时,模态成功关闭。但是,当我尝试显示响应时,模态框的黑屏并没有消失。我尝试了堆栈溢出时提供的多种解决方案(如下所示),但其中 none 对我有用:
$('#mdl-upload-file').hide();
$('#mdl-upload-file').remove();
- 将超时设置为 5000
$('#modalElement').data('modal', null);
请帮忙!
只需将 data-dismiss
添加到您的 [Upload File]
按钮即可:
<button type="button" data-dismiss="modal" disabled class="btn btn-primary" id="btn-upload-selected-file">Upload File</button>
然后去掉多余的$('#mdl-upload-file').modal('hide')
。这是不好的做法。相反,您可以阻止用户在
[Upload File]
$('#customFile').on('change',function(){
if (this.files.length>0) $('#btn-upload-selected-file').removeAttr('disabled')
})
也已将 disabled
属性添加到 [Upload File]
。