javascript - 如何从具有多个页面的表中获取表记录?
问题描述
我有一个使用 DataTables 进行分页的表。我正在尝试在 Bootstrap 模式中显示表记录。问题是我只能从只显示 10 条记录的第一页获取记录。我无法从第二页和其他页面获取记录;它一直显示我从第一页选择的最后一条记录。
我可能缺少一种从以下页面获取值的方法。
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Product Name</th>
<th scope="col">Supplier</th>
<th scope="col">Volume</th>
<th scope="col">Quantity</th>
<th scope="col">Buying Price</th>
<th scope="col">Selling Price</th>
<th scope="col">Expiry Date</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<?php include "include/read.inc.php"?>
</tbody>
</table>
$(document).ready(function() {
$('.table').DataTable();
$('.editbtn').on('click', function() {
$('#editModal').modal('show');
$tr = $(this).closest('tr');
var data = $tr.children('td').map(function() {
return $(this).text();
}).get();
console.log(data);
var str = data[3];
var res1 = str.match(/[0-9]/);
var res2 = str.match(/[a-z]+/i);
$('#id').val(data[0]);
$('#name').val(data[1]);
$('#supplier').val(data[2]);
$('#vol').val(res1);
$('#unit').val(res2);
$('#qty').val(data[4]);
$('#bprice').val(data[5]);
$('#sprice').val(data[6]);
$('#editExpDate').val(data[7]);
});
});
解决方案
感谢@freedomn-m 你的贡献,我终于找到了问题,它在于选择器行,我必须在 on('click', 'button_selector', function()); 中包含按钮选择器。看起来像这样
$('.table').on('click','.editBtn', function(){
$tr = $(this).closest('tr');
var data = $tr.children('td').map(function(){
return $(this).text();
}).get();
问候。
推荐阅读
- javascript - DataTables 在页面重新加载后保留按钮内容
- javascript - 为画布制作边界
- flutter - 如何在 Flutter 中从 name 或 lat、long 获取 PlaceID?
- angular - 路线在角度和laravel中被调用两次
- android - 创建片段的新实例时菜单未膨胀
- logging - 我可以使用 Zabbix 来监控我的应用程序生成的 JSON 日志文件吗?
- r - 使用 write.csv 和 write_csv {r} 导出到 csv 时出错
- angular - 如何修复 Angular 中的错误“unsafe:data:image/png;base64, Qk1 ...”
- reactjs - 为什么我在 CRA 应用程序上收到“未满足对等依赖性”警告?
- php - 使用 PHP 过滤 JSON 输出