简体   繁体   English

数据表操作列和jquery单击事件问题

[英]Datatable action column and jquery click event issue

I am using jquery datatable with an action column. 我正在使用带有动作列的jquery数据表。 In that action column there is two links "edit" and "delete". 在该操作列中有两个链接“编辑”和“删除”。 My table is populating correctly with this action column. 我的表格正确填充此操作列。 But problem is I need to open a bootstrap modal with a from when I click on the edit button of this datatable. 但问题是我需要在点击此数据表的编辑按钮时打开一个自举模式。 But its not open my modal. 但它没有打开我的模态。

This is how I create my action column in datatable: 这是我在datatable中创建动作列的方法:

var myTable = 
$('#view_user_table')
.DataTable({
  bAutoWidth: false,    
  "bProcessing": true,
  "bServerSide": true,
  "sAjaxSource": "includes/process_view_bank_datatable.php",
  "aoColumnDefs": [{
                    "aTargets": [6],  
                    "bSortable": false,
                    "mData": "0",   
                    "mRender": function (data, type, full) {
                       return '<div class="hidden-sm hidden-xs action-buttons">' + 
                                '<a class="green edit_this_user" href="javascript:void(0)" data-bank_id="'+data+'">edit</a>' + 
                                '<a class="red" href="javascript:void(0)">delete</a>' + 
                              '</div>';                              
                    }
                  }
                ],
  "aaSorting": [],
  "iDisplayLength": 50,

  select: {
    style: 'multi'
  }
});

Then I tried to open my modal with click on the edit link I have created above. 然后我尝试打开我的模态,点击我上面创建的编辑链接。

This is how I tried it: 这是我尝试的方式:

$('.edit_this_user').on('click', function() {
  alert('modal')  
}); 

But I cannot get the alert. 但我无法得到警报。 Can anybody tell me what is the reason to its not working? 谁能告诉我它不工作的原因是什么?

Note: I can not get any error in my console. 注意:我的控制台中不会出现任何错误。

The event binding for the dynamic generated elements, you should write in such a way that the parent content which has already present .on, then the event type then the element selector on which you want to trigger the event. 对于动态生成的元素的事件绑定,您应该以这样的方式编写已经存在.on的父内容,然后是事件类型,然后是要在其上触发事件的元素选择器。 Change the trigger code like this and try: 像这样更改触发器代码并尝试:

  $('#view_user_table').on('click', '.edit_this_user', function(){

  });

Your code does not work, because you're trying to attach event listener to html that does not exist yet. 您的代码不起作用,因为您尝试将事件侦听器附加到尚不存在的html。

You create .edit_this_user dynamically (via another script, in your example it is Datatable plugin), so when document is loaded(I suppose you're code executed on load) there is no element with such class. 你动态创建.edit_this_user (通过另一个脚本,在你的例子中它是Datatable插件),所以当加载文档时(我想你是在加载时执行的代码)没有这样的类的元素。

There are many questions about this, check them for proper explanation: 有很多问题,请检查它们是否有正确的解释:

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

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