Bootstrap 4 close modal, backdrop doesn't disa

2019-07-20 06:56发布

问题:

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>

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');
        },
    });
});

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.

I try to do this

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

Base here

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

The button:

<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>

Any help? thanks!

回答1:

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();