Javascript在数据表过滤或分页之后不工作
Javascript not working after Datatable Filter or Paging
我正在使用Django ListView来显示发票列表。我试图使用Javascript发送更新。例如,当支付发票时,用户可以单击该行的图标,该发票将被标记为已支付。一切都运行良好,但是,我使用datatable允许用户进行二次过滤。在页面加载时,javascript和对服务器的调用工作得很好,但是,当使用过滤器或分页时,javascript不拾取行号。为了简单起见,我用alert()函数对它进行了测试——下面是html
HTML:<table width="100%" class="table table-striped table-bordered table-hover" id="dt-invoice">
<thead>
<tr>
<th></th>
<th>Inoice Number</th>
<th>Description</th>
<th>Date Sent</th>
</tr>
</thead>
<tbody>
{% csrf_token %}
{% for i in invoice %}
<tr id="{{ i.invoice }}">
<td><a id='id_paid-{{ i.id }}' title="I Got This!" href="#"><i class="fa fa-check fa-fw"></i></a></td>
<td>i.invoice_number</td>
<td>i.invoice_description</td>
<td>i.invoice_sent_date</td>
</tr>
{% endfor %}
</tbody>
</table>
Javascript: $(document).ready(function() {
$('#dt-invoice').DataTable({
buttons: [ 'copy', 'csv', 'excel', 'pdf','print'],
"iDisplayLength": 10,
"processing": true,
responsive: true,
"dom": '<"top pull-left" li><"top pull-right" f> <t> <"bottom pull-left" iB><"bottom pull-right" p>',
});
$('[id^=id_paid-]').click(function(){
console.log("HERE")
row = $(this).closest('tr');
trid = row.attr("id");
alert(trid);
});
});
任何想法?提前感谢!
你应该把点击事件委托给最近的静态容器:
$('#dt-invoice').on('click', '[id^=id_paid-]', function() {
console.log("HERE")
row = $(this).closest('tr');
trid = row.attr("id");
alert(trid);
});
忽略click事件的原因是因为在绑定它时,只有DOM中可用的匹配元素才能获得事件绑定。Datatable插件修改table
内容,删除/添加分页/过滤的新元素。通过将其绑定到table
元素(最接近的静态元素),您也可以处理所有这些动态元素。
- 代表团
- jQuery .fn.on美元()
相关文章:
- 应用程序在“关闭”之后停止工作(控制台中没有错误);咕哝的构造”;
- 有时 array.length 只在 .push() 之后工作(为什么?)
- ASPxClientGridView之后激发的客户端事件.ApplyFilter执行其工作
- 地图,rails 4.2,javascript,鼠标悬停,只工作一次(或两次).然后在重新加载之后
- Javascript在此代码之后停止工作
- 我的jquery代码在谷歌chrome控制台中工作,而不是在保存它之后
- 在表单提交将DOM的一部分替换为分部之后,我应该将ajax成功绑定到什么来使我的javascript工作
- 全局变量确实'在script标记中的外部.js文件之后,nt工作
- 为什么在Firefox更新之后JQuery不能在我的MVC 4应用程序中工作
- 为什么我之前工作的谷歌可视化图表在2013年11月26日之后停止工作
- HTML分页在第一页上搞砸了,之后它工作正常
- AngularJS$index在orderBy之后无法正常工作
- 在 .detach() 和 .append() 之后使用 .load() 无法按预期工作
- Gulp-rev:不是第一次工作,而是在第一次工作之后工作
- JS仅在控制台.log之后工作
- jQuery 模式对话框在刷新后的第一页上工作正常,但在此之后的任何其他页面上都无法正常工作,除非刷新
- $.post之后jQuery日期选择器不工作
- fadeIn作为fadeOut之后的回调函数没有按预期工作
- 为什么这个JavaScript函数在网站第一次加载时不能正确计算,但之后工作正常?
- Javascript不能在Ajax之后工作