Ajax stops working when HTML is reloaded with form

0

I have a Store on my page that corresponds to a wordpress plugin. To improve the user experience I decided to use AJAX in paging, which the plugin does not include (if it includes ajax for the add buttons to the cart).

The issue is that when I changed pages, I did the rendering also of the navigation and I was recharging everything when doing the second click on a new page. I read that it is best to reload as HTML what you do not interact with so that ajax works.

The issue is that every time I reload the products I also reload the add button to the cart. Then, after I change the page the first click on "Add to Cart" I reload the entire page again, and the following interactions AJAX works, as long as the page does not change.

It's a lot of code, but I'll try to place the most representative one (the onclic event to add to the cart):

jQuery(document).ready(function($) {
    $(function() {        
        /*** Handle add to cart action ***/
        $('.eStore-button-form').on('submit', function() {
           //Hace un montón de cosas
}
});


<form method="post" action="" 
      class="eStore-button-form eStore-button-form-24" 
      style="display:inline" 
      onsubmit="return ReadForm1(this, 1);">

<div class="eStore_variation_top"></div>
<input type="submit" value="Comprar" 
     class="eStore_button eStore_add_to_cart_button">

</form>

Any ideas?

* Update *

//Clic en Paginación de Productos
    $('.pagination_page').click(function(){
        href_clic = $(this).attr('href');

        var product_page = $(this).attr("href").match(/product_page=([0-9]+)/)[1];

        //Actualiza la dirección URL. Siempre tiene que ir con un return false después.
        window.history.pushState('obj', 'PageTitle', '/tienda/?product_page=' + product_page);

        jQuery.ajax({
            url : vc.ajaxurl,
            type : "post",
            dataType: "json",
            data : {
                action : 'vc_eStore_paginacion',
                product_page: product_page,
                nonce: vc.vc_eStore_nonce,
            },
            beforeSend: function(){

            },
            error: function(jqXHR, textStatus, errorThrown){
                console.log("jqXHR: " + jqXHR);
                console.log("textStatus: " + textStatus);
                console.log("errorThrown: " + errorThrown);
            },
            success : function(respuesta) {

                console.log('ejecución .pagination_page');
                console.log(respuesta);

                $('.eStore-fancy-wrapper').remove();

                var html_productos = $(respuesta.html_product_page).not('.eStore_pagination');
                $(html_productos).insertBefore( ".eStore_pagination" );
            }
        });

        return false;      
    });

html_productos is the product grid with its photo and its button form that are loaded again in the section of the page.

    
asked by vivi 31.10.2018 в 15:03
source

2 answers

0

Try to assign the previous click with off , which I think is the problem, drag that event.

   $('.pagination_page').off('click').on('click', function(){
        href_clic = $(this).attr('href');

        var product_page = $(this).attr("href").match(/product_page=([0-9]+)/)[1];

        //Actualiza la dirección URL. Siempre tiene que ir con un return false después.
        window.history.pushState('obj', 'PageTitle', '/tienda/?product_page=' + product_page);

        jQuery.ajax({
            url : vc.ajaxurl,
            type : "post",
            dataType: "json",
            data : {
                action : 'vc_eStore_paginacion',
                product_page: product_page,
                nonce: vc.vc_eStore_nonce,
            },
            beforeSend: function(){

            },
            error: function(jqXHR, textStatus, errorThrown){
                console.log("jqXHR: " + jqXHR);
                console.log("textStatus: " + textStatus);
                console.log("errorThrown: " + errorThrown);
            },
            success : function(respuesta) {

                console.log('ejecución .pagination_page');
                console.log(respuesta);

                $('.eStore-fancy-wrapper').remove();

                var html_productos = $(respuesta.html_product_page).not('.eStore_pagination');
                $(html_productos).insertBefore( ".eStore_pagination" );
            }
        });

        return false;      
    });
    
answered by 31.10.2018 в 16:04
0

Finally I have solved it. so that my element does not lose the listener when it is recreated, I have assigned it to its parent container that remains unchanged on the page.

This is what I changed:

$('#product-grid').on('click', '.pagination_page', function (){...

Instead of:

$('.pagination_page').click(function(){...

I hope it helps those who find themselves with this same problem! Once you understand AYAX it is a pleasure to see what can be achieved.

Thank you!

    
answered by 31.10.2018 в 18:56