简体   繁体   English

jQuery 数据表事件未触发

[英]jQuery DataTable events not firing

I have a datatable that i dynamically initialise thru ajax's call and i have attached the click event on the table cell(td) hover when i try to click the cell i don't get the click event being fired, i am wondering what could be the cause.我有一个数据表,我通过 ajax 的调用动态初始化,并且我在表格单元格(td)hover 上附加了点击事件,当我尝试点击单元格时,我没有触发点击事件,我想知道可能是什么原因。 Here is the snippet这是片段

 let table = $('#data-table').DataTable(); $(document).ready(function() { $("#data-table tbody").on("click", "td", function () { console.log('Datatable was clicked') }); }); window.onload = function() { table = $('#data-table').DataTable({ destroy: true, responsive: true, serverSide: false, autoWidth: false, paging: true, filter: true, searching: true, stateSave: true, scrollX: true, lengthMenu: [5,10, 25, 50, 75, 100], ajax: { url: 'https://jsonplaceholder.typicode.com/todos', type: 'GET', dataSrc: '' }, columns: [ { title: 'User Id', data: 'userId', }, { title: 'Id', data: 'id', }, { title: 'Title', data: 'title', } ] }); }
 <link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css"> <table id="data-table" data-order='[[1,"asc"]]' class="table table-bordered table-hover"> <thead> <tr class="primary"> <th>User Id</th> <th>Id</th> <th>Title</th> </tr> </thead> <tbody></tbody> </table> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"> </script>

There are a number of issues that really just boil down to typo's and bad syntax.有许多问题实际上归结为拼写错误和语法错误。 The main take-home from this is to use the browser's console (it literally tells you what the problem is).主要的收获是使用浏览器的控制台(它会告诉你问题出在哪里)。

Here's a cleaned up version.这是一个清理后的版本。 Note that I've manually added a row of td s since your event listener is listening for clicks on tbody>td and there are none until and unless the ajax returns some (which it won't in a snippet)请注意,我已经手动添加了一行td ,因为您的事件侦听器正在侦听tbody>td上的点击,除非 ajax 返回一些(它不会在片段中)

 let table; $(function(){ $("#data-table tbody").on("click", "td", function(){ console.log('Datatable was clicked') }); table = $('#data-table').DataTable({ destroy: true, responsive: true, serverSide: false, autoWidth: false, paging: true, filter: true, searching: true, stateSave: true, scrollX: true, lengthMenu: [5,10, 25, 50, 75, 100], ajax: { url: 'https://jsonplaceholder.typicode.com/todos', type: 'GET', dataSrc: '' }, columns: [ { title: 'Zone', data: 'LastKnownZone', }, { title: 'Hiérarchie Map', data: 'MapInfo.mapHierarchyString', }, { title: 'Addresse MAC', data: 'macAddress', } ] }); });
 <link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css"> <table id="data-table" data-order='[[1,"asc"]]' class="table table-bordered table-hover"> <thead> <tr class="primary"> <th>Zone</th> <th>Hiérarchie Map</th> <th>Addresse MAC</th> </tr> </thead> <tbody><tr class="fordemopurposes"> <td>1</td> <td>2</td> <td>3</td> </tr></tbody> </table> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"> </script>

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

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