简体   繁体   中英

page scrolls to top on modal popup

I am developing a shopping cart using bootstrap(front end) and codeigniter. when add to cart item modal popup shows the details of added item. But my issue is page scrolls moves to top on modal popup.

<a href="#" id="edit_product" data-id="<?php echo $lat['product_id'];?>" name="id" data-text="Add To Cart" class="my-cart-b item_add add_to_cart">Add To Cart</a>

Ajax

$(".product_add").click(function(event) {

var currentId = globalId;


 $.ajax({
            type: 'POST',
            url: '<?php echo site_url("ajax_controller1/product_add/'+currentId+'")?>',
            data: { id:'1' }, 
            success:function(response){
              $("#cart_container1").html(response);
                $("#myModal3").modal('show');
     }
  });/* Aajx */



}); 

Modal

<div class="modal fade" id="myModal3" tabindex="-1" role="dialog" style="overflow: visible;">
                <div class="modal-dialog" role="document" style="overflow: visible;">
                    <div class="modal-content modal-info" style="overflow: visible;">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>                        
                        </div>
                        <div class="modal-body" id="cart_container1">

                        </div>
                    </div>
                </div>
            </div>

I have done the following to overcome this issue

$('a.add_to_cart').click(function(e) {
    e.preventDefault();
});

body.modal-open {
    overflow: visible;
}

This is my demo website link

http://cableandmedia.com/ayurstore/

I feel the culprit is href="#"

What you can do 2 things,

  1. Just remove href="#"
  2. Change <a> to `

    <button type="button" id="edit_product" data-id="<?php echo $lat['product_id'];?>" name="id" data-text="Add To Cart" class="my-cart-b item_add add_to_cart">Add To Cart</button>

This should help you.

Notice that i have added type="button" if you are following 2nd approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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