简体   繁体   English

点击事件未在第二页上注册

[英]Click event not registering on second page

I'm using tablesorter and tablesorter.pager. 我正在使用tablesorter和tablesorter.pager。 Here is my code. 这是我的代码。

$(document).ready(function() {
$("#peopletable")
        .tablesorter({ widthFixed: true, widgets: ['zebra'] })
        .tablesorterFilter({ filterContainer: $("#people-filter-box"),
            filterClearContainer: $("#people-filter-clear-button"),
            filterColumns: [1, 2, 3],
            filterCaseSensitive: false
        })
        .tablesorterPager({ container: $("#peoplepager") });

$("#peopletable tr.data").click(function() {
    var personid = $(this).attr('id');
    $.ajax({
    type: "POST",
    url: "/Search/GetDocumentsByPerson",
    data: { "id": personid },
    datatype: "json",
    success: function(data) {
        var results = eval(data);
        $("#documentstable > tbody tr").remove();
        $.each(results, function(key, item) {
            $("#documentstable > tbody:last").append(html);
        });
        $("#documentstable").trigger("update");
    }
});
});
});

Everything works great except when I click on the next page my button click event doesn't fire. 一切正常,但当我单击下一页时,按钮单击事件不会触发。 Is this a known issue with jquery tablesorter? 这是jquery tablesorter的已知问题吗?

It's because the elements are updated, the ones you bound the click handler to are gone, you can use .live() to resolve this, change this: 这是因为元素已更新,绑定了单击处理程序的元素已消失,您可以使用.live()来解决此问题,更改此问题:

$("#peopletable tr.data").click(function() {

To this: 对此:

$("#peopletable tr.data").live('click', function() {

Alternatively, if #peopletable isn't destroyed you can use .delegate() , like this: 或者,如果#peopletable没有被破坏,则可以使用.delegate() ,如下所示:

$("#peopletable").delegate('tr.data', 'click', function() {

I have also faced the same kind of problem with tablesorterPager second page after using Jeditable (edit in place) plugin for some element in the tablesorterPager used table. 在使用Jeditable(就地编辑)插件为tablesorterPager用过的表中的某些元素使用了tablesorterPager第二页后,我也遇到了同样的问题。

I have tried editing the data bind function in Jeditable as follows 我已经尝试过在Jeditable中编辑数据绑定函数,如下所示

original code 原始码

 $(this).bind(settings.event, function(e) {

here settings.event equals to the event parameter which we are defining with options eg: click 这里settings.event等于我们用选项定义的事件参数,例如:click

modified code 修改后的代码

 $(this).live(settings.event, function(e) {

But.. I found the error with tablesorterPager within pages other than the first page is not because of the binding of element event. 但是..我发现除第一页之外的其他页面内的tablesorterPager错误不是由于元素事件的绑定。 when we are calling tablesorterPager to any table with many rows, only the first page rows of the table is affected on the page load. 当我们向具有许多行的任何表调用tablesorterPager时,仅页面的第一页行会受到页面加载的影响。 so only the first page rows are called with Jeditable plugin. 因此只有第一行会通过Jeditable插件调用。 other rows in the other pages are not assigned with the plugin. 其他页面中的其他行未分配该插件。 because of this reason, the events in other pages than first page will not work. 因此,首页以外的其他页面中的事件将不起作用。

to prevent above situation, we can add Jeditable plugin calling inside updatePageDisplay function. 为了防止出现上述情况,我们可以在updatePageDisplay函数内部添加Jeditable插件。

eg: 例如:

function updatePageDisplay(c) {

    $(".tablerowdata").each(function(){

            $(this).editable("ajax/save.php", {
              tooltip : "click to edit...",
              data   : {"selectid1":"selectval1","selectid2":"selectval2","selectid3":"selectval3"},
              type   : "select",
              submit : "ok",
              event     : "click",
              select : "true",
            });
          });

创建一个新元素不会复制用click方法创建的事件,而live方法会这样做。

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

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