简体   繁体   English

模态在背景上无法正确关闭

[英]Modal not closing correctly on backdrop

I have a problem with my modal when opened I am able to close it via the close button or X button, however would also like to close it on the backdrop. 我的模态打开时出现问题,我可以通过关闭按钮或X按钮将其关闭,但是也想在背景中将其关闭。 The backdrop will close the modal. 背景将关闭模式。 However, when i click on another item to open another modal, the modal that had been previously closed through the backdrop will only show. 但是,当我单击另一个项目以打开另一个模态时,先前已通过背景关闭的模态将仅显示。

<div class="modal fade details-1 " id="details-modal" tabindex="-1" role="dialog" aria-label="details-1" aria-hidden="true" data-dismiss="modal">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" type="button" onclick="close_modal();" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <!-- all going to be dynamic later -->
                    <h4 class="modal-title" class="text-center"><?php echo $product['title']; ?></h4>
                </div>
                <div class="modal-body">
                    <!-- we are going to put container fluid inside the modal body so we can
                    give it cols -->
                    <div class="container-fluid">
                        <div class="row">
                            <span id="modal_errors" class="bg-danger"></span>
                            <div class="col-sm-6 fotorama"  data-nav="thumbs">
                                    <?php $photos = explode(',',$product['image']);
                          foreach($photos as $photo): ?>
                                        <img src="<?= $photo; ?>" alt="<?= $product['title'] ?>" class="details img-responsive auto">
                                        <?php endforeach; ?>

                            </div>
                            <div class="col-sm-6">
                                <h4>Details</h4>
                                <p><?= nl2br($product['description']); ?></p>
                                <hr>
                                <p>Price: &pound;<?= $product ['price']; ?></P>
                                    <p>Brand: <?= $brand['brand']; ?></p>
                                <form action="add_cart.php" method="post" id="add_product_form">
                                    <input type="hidden" name="product_id" id="product_id" value="<?=$id;?>">
                                    <input type="hidden" name="available" id="available" value="">
                                    <div class="form-group">
                                        <!-- Allows us to control width of input -->
                                    </div>
                                    <div class="form-group">
                                        <label for="size">Size: </label>
                                        <select name="size" id="size" class="form-control">Size
                                            <option value=""></option>
                                            <?php foreach($size_array as $string) {
                                                $string_array = explode(':', $string);
                                                $size = $string_array[0];
                                                $available = $string_array[1];
                                                if($available > 0){
                                                echo '<option value="'.$size.'" data-available="'.$available.'">'.$size.' ('.$available.' Available)</option>';
                                                }
                                             } ?>
                                        </select>
                                    </div><br><br>
                                    <div class="col-xs-3 pull-right">
                                        <label for="quantity">Qty: </label>
                                        <input type="number" class="form-control" id="quantity" name="quantity"  min="0">
                                    </div><div class="col-xs-9"></div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-default" onclick="close_modal();">Close</button>
                    <button class="btn btn-deafault" type="submit" onclick="add_to_cart();return false;"><span class="glyphicon glyphicon-shopping-cart"></span>Add to Cart</button>
                </div>
            </div>
        </div>
    </div>
    function close_modal() {
    jQuery('#details-modal').modal('hide');
    setTimeout(function() {

        jQuery('#details-modal').remove();
        jQuery('.modal-backdrop').remove();
    },300);
}

You shouldn't need to write your own logic to close the modal when clicking the X or the background, twitter bootstrap does that by default as long as you have your html marked up correctly. 单击X或背景时,您无需编写自己的逻辑来关闭模式,Twitter引导程序默认情况下会执行此操作,只要您正确标记了html。

Remove the onclick handler and the close_modal function altogether and if you need a reference for fixing your markup, you can find a working example of multiple modals on the same page on the twitter bootstrap documentation for modals . 完全删除onclick处理程序和close_modal函数,如果您需要参考来修复标记,则可以在twitter引导文档的模态页面的同一页面上找到多个模态的工作示例。

This is all you need to close the modal. 这是关闭模式所需的全部。 Just bind this to an event. 只需将此绑定到一个事件。

jQuery('#details-modal').modal('hide');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM