Bootstrap 4关闭模式,背景不会消失 [英] Bootstrap 4 close modal, backdrop doesn't disappear

查看:89
本文介绍了Bootstrap 4关闭模式,背景不会消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用boostrap 4,所以我有

I am using boostrap 4 so I have

css:

<link rel="stylesheet" type="text/css" href="//stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">

js:

<script src="//stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>

所以我有模态,并且里面是产品的复选框.

So I have modal and inside of it are checkboxes for product.

<div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="smallmodalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body items-container products">
            <div class="row form-group">
                <div class="col col-sm-6 pr-1">
                    <select data-placeholder="Choose Brand..." multiple class="standard-select mb-3">
                        <option value=""></option>
                        <option value="1">Brand 1</option>
                        <option value="1">Brand 2</option>
                    </select>
                </div>
                <div class="col col-sm-6 pl-0">
                    <input type="text" id="search_by_name" placeholder="Search by Name" class="form-control">
                </div>
            </div>
            <div id="product_checkboxes" style="margin-top: 45px;position:relative;">
                <div class="partial-loader hide"></div>
                <div class="checkbox">
                    <label class="checkbox-bootstrap checkbox-lg">                           
                        <input type="checkbox" class="product-checkbox" name="product" value="1">             
                        <span class="checkbox-placeholder"></span>           
                        Sample Item
                    </label>
                </div>
                <div class="checkbox">
                    <label class="checkbox-bootstrap checkbox-lg">                           
                        <input type="checkbox" class="product-checkbox" name="product" value="2">             
                        <span class="checkbox-placeholder"></span>           
                        Sample Item 2
                    </label>
                </div>
            </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="add_selected_button" disabled>Add Selected Products</button><img class="hide loader" src="/logo/loader.gif" />
      </div>
    </div>
  </div>
</div>

在我的Js文件中,我有关于add_selected_button的事件

On my Js file I have event for add_selected_button

$(modalJs.addSelectedButtonSelector).click(function() {
    var me = $(this);

    me.attr('disabled', 'disabled');
    $(modalJs.chosenSelector).prop('disabled', true).trigger("chosen:updated");
    $(modalJs.inputSearchTextSelector).attr('disabled', 'disabled');
    $('.btn-secondary').attr('disabled', 'disabled');

    var selectedId = [];
    $(modalJs.productCheckboxesSelector +' input:checked').each(function() {
        selectedId.push(parseInt($(this).val()));
    });

    var loader = $('.products .partial-loader');
    loader.removeClass(core.common.classToHideElement);
    $('img.loader').show();

    var csrfToken = $('meta[name="csrf-token"]').attr("content");
    var store = $.ajax({
        url: "/product/store",
        type: "PUT",
        dataType: 'json',
        data: {
            product_ids: selectedId, 
            _csrf : csrfToken
        },
        success: function (response) {
            $('#modal').modal('hide');
        },
    });
});

我尝试在ajax请求完成并成功后手动隐藏模式. 根据他们的网站放置此代码

I am trying to hide the modal manually after ajax request done and successful. according to their website just put this code

$('#myModal').modal('hide')

它关闭了模态,但是背景并没有消失.

It close the modal, however the backdrop doesn't disappear.

我尝试这样做

$('body').removeClass('modal-open');
$('.modal-backdrop').remove();

此处

它起作用了,背景消失了,但是除非我刷新页面,否则显示模式的按钮将不再起作用.

It works, backdrop is disappear, however the button to show the modal is not working anymore unless I refresh the page.

按钮:

<button class="btn btn-primary ml-2" data-toggle="modal" data-target="#modal">
    <i class="fa fa-plus" aria-hidden="true"></i>
    Add Products
</button>

有帮助吗?谢谢!

推荐答案

BS还将类添加到< body>需要删除的元素(请参见此线程).尝试以下代码:

BS also adds a class to the <body> element that needs to be removed (see this thread). Try this code:

$('#myModal').modal('hide');
$(document.body).removeClass('modal-open');
$('.modal-backdrop').remove();

这篇关于Bootstrap 4关闭模式,背景不会消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆