Jquery submit doesn't work after ajax pagination

I am using ajax pagination script which works great - https://github.com/gbirke/jquery_pagination

Everything works fine on the first page, each page has submit buttons (multiple forms) that submit data to the database via a jquery POST.

However, the jquery post only works for forms / buttons on the first page loaded. If you go to another page on the page, the jquery post / submit button doesn't work.

Any suggestions would be highly appreciated, can't seem to make it clear.

Snippets of code:

Paging Scripts

<script>
    $(document).ready(function() {
        $("#Pagination").trigger('setPage', [<?php echo $member_Program_Week_calc; ?>]);
    });
</script>

 <script type="text/javascript">
    function pageselectCallback(page_index, jq){
        var new_content = jQuery('#hiddenresult div.result:eq('+page_index+')').clone();
        $('#Searchresult').empty().append(new_content);
        return false;
    }

    function initPagination() {
        // count entries inside the hidden content
        var num_entries = jQuery('#hiddenresult div.result').length;
        // Create content inside pagination element
        $("#Pagination").pagination(num_entries, {
            callback: pageselectCallback,
            items_per_page:1 // Show only one item per page
        });
     }

    // When document is ready, initialize pagination
    $(document).ready(function(){      
        initPagination();
    });
 </script>

      

AjaxSubmit function

<script type="text/javascript">
$(document).ready(function() {

    $('#completetask_<?php the_sub_field('tasks_id'); ?>').submit(ajaxSubmit); // example

        function ajaxSubmit(){

                $(this).find(':submit').attr('disabled','disabled');

                var completetask = jQuery(this).serialize();

                jQuery.ajax({
                        type:"POST",
                        url: "/wp-admin/admin-ajax.php",
                        data: completetask,
                        success:function(data){
                jQuery("#feedback").html(data);
                        },
                error: function(errorThrown){
                    alert(errorThrown);
                }  
                });
                return false;
        }
    });
</script>

      

Form code

<form method="post" id="completetask_<?php echo $task_id; ?>">
    <input type="hidden" name="program_id" id="program_id" value="<?php echo $program_id; ?>"/>
    <input type="hidden" name="session_id" id="session_id" value="<?php echo $session_id; ?>"/>
    <input type="hidden" name="task_id" id="task_id" value="<?php echo $task_id; ?>"/>
    <input type="hidden" name="action" value="completeTask"/>

        <input class="program_task_submit_no program_task_submit_no_<?php echo $task_id; ?>" type="submit" value="COMPLETE TASK"/>

</form>

      

-1


source to share


1 answer


I was able to solve this thanks to a suggestion from @charlietfl and a little further research.

The solution is event delegation using the .on () function.

With my code, I changed the code of the ajaxSubmit function using the .on function.

Simple example:

$(document).on('submit', '#completetask', function(e) {
    //CODE
});

      

My updated code:



$(document).on('submit', '#completetask_<?php the_sub_field('tasks_id'); ?>', function(e) {

    $(this).find(':submit').attr('disabled','disabled');

    var completetask = jQuery(this).serialize();

    var css = document.createElement("style");
    css.type = "text/css";
    css.innerHTML = ".program_task_title_<?php the_sub_field('tasks_id'); ?> { color: green !important; }";

    document.body.appendChild(css);

    jQuery.ajax({
            type:"POST",
            url: "/wp-admin/admin-ajax.php",
            data: completetask,
            success:function(data){
    jQuery("#feedback").html(data);
            },
    error: function(errorThrown){
        alert(errorThrown);
    }  
    });
    return false;

});

      

I removed the source code of the ajaxSubmit function.

Working with CSS

I also needed the CSS to execute after the button was submitted, which changed the style of the form. However, by changing the reset page, this will revert to normal. For this I have styled the CSS in the script.

var css = document.createElement("style");
css.type = "text/css";
css.innerHTML = ".program_task_title_<?php the_sub_field('tasks_id'); ?> { color: green !important; }";

document.body.appendChild(css);

      

Thank.

0


source







All Articles