最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Datatables and Modals - Stack Overflow

programmeradmin4浏览0评论

I have a data table setup (/) and I am trying to have an option to delete a row from the table and have a jQuery dialog modal popup to confirm. However, the modal works fine on the first page of the table, but when the table goes to the 2nd page, the modal stops working. My guess is when the table paginates to the next 10 entries, something happens that removes the dialog click call.

Here is my code for the modal:

    $('#delete-dialog').dialog({
        autoOpen: false, 
        width: 400,
        modal: true
    });

    $('.delete_modal').click(function (e) {
        e.preventDefault();
        var targetUrl = $(this).attr("href");
        $('#delete-dialog').dialog({
          buttons: {
            "Delete": function() {
              window.location.href = targetUrl;
            },
            "Cancel": function() {
              $(this).dialog("close");
            }
          }
        });
        $('#delete-dialog').dialog("open");
    });

and here is my code for the modal:

<div id="delete-dialog" title="Remove User">
    <p>Are you sure you want to delete this user?</p>
</div>

Any idea why it works on the first page of the table, but not the 2nd?

Here is my delete code:

<li><a href="?page=users&sub=admins&action=delete&id=<?=$row['id']?>" class="delete_modal"><span class="icos-trash"></span>Delete</a></li>

I have a data table setup (http://datatables/) and I am trying to have an option to delete a row from the table and have a jQuery dialog modal popup to confirm. However, the modal works fine on the first page of the table, but when the table goes to the 2nd page, the modal stops working. My guess is when the table paginates to the next 10 entries, something happens that removes the dialog click call.

Here is my code for the modal:

    $('#delete-dialog').dialog({
        autoOpen: false, 
        width: 400,
        modal: true
    });

    $('.delete_modal').click(function (e) {
        e.preventDefault();
        var targetUrl = $(this).attr("href");
        $('#delete-dialog').dialog({
          buttons: {
            "Delete": function() {
              window.location.href = targetUrl;
            },
            "Cancel": function() {
              $(this).dialog("close");
            }
          }
        });
        $('#delete-dialog').dialog("open");
    });

and here is my code for the modal:

<div id="delete-dialog" title="Remove User">
    <p>Are you sure you want to delete this user?</p>
</div>

Any idea why it works on the first page of the table, but not the 2nd?

Here is my delete code:

<li><a href="?page=users&sub=admins&action=delete&id=<?=$row['id']?>" class="delete_modal"><span class="icos-trash"></span>Delete</a></li>
Share asked Aug 15, 2012 at 20:18 Pat HerlihyPat Herlihy 1734 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

My guess is that you're loading new elements that have not been bound with jquery. You could bind the new elements or attach a handler to you .delete_modal class like so.

$("body").on("click", ".delete_modal", function (e) {
e.preventDefault();
        var targetUrl = $(this).attr("href");
        $('#delete-dialog').dialog({
          buttons: {
            "Delete": function() {
              window.location.href = targetUrl;
            },
            "Cancel": function() {
              $(this).dialog("close");
            }
          }
        });
        $('#delete-dialog').dialog("open");
});
发布评论

评论列表(0)

  1. 暂无评论