PHP/Ajax/jquery 操作在分页点击后不起作用



我有一个php页面,它使用jquery/ajax加载一个带有分页的表。单击分页中的任何页面(初始页面除外(后,我无法将行重定向到其他页面

这是我为表格生成的 html 的简化代码。我知道每次更改分页页面时都会正确生成 html。

<tr class="table-row" data-myredirect="http://example.com/page">
<td>blah blah</td>
</tr>

成功加载表数据后(仅在用户运行查询后运行一次(,我运行以下内容(简化(:

success:function(result){
         //display table data (not shown)
$(".table-row").click(function() {
         //redirect
        window.document.location = $(this).data("myredirect");
    });
}

当用户从分页链接中选择其他页面时,将运行以下内容:

//executes code below when user click on pagination links
    $("#results").on( "click", ".pagination a", function (e){
        e.preventDefault();
        $(".loading-div").show(); //show loading element
        var page = $(this).attr("data-page"); //get page number from link
        $("#results").load("test.php",{"page":page, "myparam":searchItemOne}, function(){ //get content from PHP page
            $(".loading-div").hide(); //once done, hide loading element
            //fixes scrolling up bug
            $('html, body').animate({
        scrollTop: $("#results").offset().top
     }, 0);
        });
    });

导航到下一个分页页面后,可能导致操作不再起作用的原因是什么?我可以根据要求包含更多代码。

我认为这是因为

您的.table-row是在javascript代码之后呈现的(在您第二次通过AJAX加载html之后(,因此重定向代码不知道新的table-row
尝试放置重定向代码

$(".table-row").click(function() {
         //redirect
        window.document.location = $(this).data("myredirect");
    });

php,以便它使用新的 HTML
重新加载。

>Taki的回答并没有提供解决方案,而是提供了指南。

为了解决这个问题,我在定义 $(".table-row").click() 事件后将$('#results').DataTable();移到了 javascript 的末尾。现在我所有的页面都会触发嵌入式功能。

最新更新