【问题标题】:The modal screen remains black on showing AJAX response after modal hide模态隐藏后显示 AJAX 响应时模态屏幕保持黑色
【发布时间】: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">&times;</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) 时,模式会成功关闭。但是,当我尝试显示响应时,模态的黑屏并没有消失。我尝试了针对堆栈溢出提供的多种解决方案(如下所示),但没有一个对我有用:

  1. $('#mdl-upload-file').hide();
  2. $('#mdl-upload-file').remove();
  3. 将超时设置为 5000
  4. $('#modalElement').data('modal', null);

请帮忙!

【问题讨论】:

  • 你需要调用 bootstrap 的模态隐藏函数。 $('#mdl-upload-file').modal("hide");
  • 我在点击按钮时调用它,查看javascript代码
  • 我试过$('#modalElement').data('modal', null);,但没用。
  • 这不是页面上唯一的解决方案...

标签: jquery twitter-bootstrap modal-dialog


【解决方案1】:

只需将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]

【讨论】:

    猜你喜欢
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多