简体   繁体   English

Datatable jQuery第二页记录删除事件不起作用

[英]Datatable Jquery second page record delete event does not work

I am using datatable jquery for my records fetched from database and i wanted to perform edit and delete event for each records. 我正在对从数据库中获取的记录使用datatable jQuery,我想对每个记录执行编辑和删除事件。

The datatable is paginated and I am facing problem with delete function. 数据表是分页的,我正面临删除功能的问题。

For the delete function on first page It gets sucesfully executed as shown in the image below 对于首页上的删除功能,它将成功执行,如下图所示

从第一次分页调用delete函数

This is my second pagination page and it does not even call the function 这是我的第二个分页页面,它甚至没有调用该函数

第二次分页

This is my html code 这是我的html代码

<a class="supp_delete_link" id="<?php echo $user->ID; ?>">
              <i class="link black remove icon"></i>
            </a></td>

This is my javascript code 这是我的JavaScript代码

<script type="text/javascript">
         $(function(){
           $(".supp_delete_link").click(function(){
             debugger;
             var element = $(this);
             var del_id = element.attr("id");
             console.log(del_id);
             var info = del_id;
         if(confirm("Are you sure you want to delete this?"))
         {
          $.ajax({
            type: "GET",
            url: "supplierdelete"+'/'+info,
            data: info,
            success: function(){
          }
         });
           $(this).parents(".show").animate({ backgroundColor: "#003" }, "slow")
           .animate({ opacity: "hide" }, "slow");
          }
         return false;
         });
         });
</script>

The above code works perfectly when I am on the first pagination but does not at all called when on the second page. 当我在第一个分页上时,上面的代码完美地工作,而在第二个页面上时根本没有调用。

More creepy is, it does not even show any error in console. 更令人毛骨悚然的是,它甚至在控制台中都没有显示任何错误。 I am stuck. 我被困住了。

Bind on event will work.. 绑定事件将起作用。

<script type="text/javascript">
     $(function(){
       $(document).on("click",".supp_delete_link",function(){//change this line on event bind
         debugger;
         var element = $(this);
         var del_id = element.attr("id");
         console.log(del_id);
         var info = del_id;
     if(confirm("Are you sure you want to delete this?"))
     {
      $.ajax({
        type: "GET",
        url: "supplierdelete"+'/'+info,
        data: info,
        success: function(){
      }
     });
       $(this).parents(".show").animate({ backgroundColor: "#003" }, "slow")
       .animate({ opacity: "hide" }, "slow");
      }
     return false;
     });
     });

I solved it with changing 我通过改变解决了

$(function(){
       $(".supp_delete_link").click(function(){.....});

Use This code below 在下面使用此代码

$(document).on('click', '.supp_delete_link', function(e){
e.preventDefault();......});

You should select table also while binding an event, For example: 绑定事件时,还应该选择表,例如:

let my_table = $('#tableID').DataTable();

$('#tableID').on('click', ".supp_delete_link", function(event){

event.preventDefault();

// Put here your rest logic

});

This is not calling because, you are loading content from AJAX. 这不是因为您正在从AJAX加载内容。

And events (update, delete) are only bound to initially loaded content. 事件(更新,删除)仅绑定到最初加载的内容。

So, basically, events are not binding. 因此,基本上,事件没有约束力。

You need to modify your function so that events get bound even for dynamically loaded content. 您需要修改函数,以便事件即使对于动态加载的内容也被绑定。

jQuery .on() is such method which will bind events to your dynamically loaded content also. jQuery .on()是一种将事件也绑定到动态加载的内容的方法。

Modified code: 修改后的代码:

<script type="text/javascript">
         $(function(){
           $(".supp_delete_link").on('click', function(){
             debugger;
             var element = $(this);
             var del_id = element.attr("id");
             console.log(del_id);
             var info = del_id;
         if(confirm("Are you sure you want to delete this?"))
         {
          $.ajax({
            type: "GET",
            url: "supplierdelete"+'/'+info,
            data: info,
            success: function(){
          }
         });
           $(this).parents(".show").animate({ backgroundColor: "#003" }, "slow")
           .animate({ opacity: "hide" }, "slow");
          }
         return false;
         });
         });
</script>

jQuery API .on() jQuery API .on()

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

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