gpt4 book ai didi

数据表过滤器或分页后 JavaScript 不工作

转载 作者:行者123 更新时间:2023-12-03 05:54:53 26 4
gpt4 key购买 nike

我正在使用 Django ListView 显示发票列表。我正在尝试使用 Javascript 发送更新。例如,当发票已付款时,用户可以单击该行的图标,该发票将被标记为已付款。一切都运行良好,但是,我使用数据表来允许用户进行二次过滤。在页面加载时,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);
});
});

有什么想法吗?提前致谢!

最佳答案

您应该将点击事件委托(delegate)给最近的静态容器:

$('#dt-invoice').on('click', '[id^=id_paid-]', function() {
console.log("HERE")
row = $(this).closest('tr');
trid = row.attr("id");
alert(trid);
});

您的点击事件被忽略的原因是因为在绑定(bind)它时,只有 DOM 中可用的匹配元素才会绑定(bind)事件。数据表插件修改table内容,在分页/过滤上删除/添加新元素。通过将其绑定(bind)到 table 元素(最接近的静态元素),您也可以处理所有这些动态元素。

[引用文献]

关于数据表过滤器或分页后 JavaScript 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39976359/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com