Javascript 从jQuery DataTable将行数据获取到数组中时出现问题
请查看并告诉我为什么无法将所选行放入数组中? 他的代码是我用来获取数据的代码:Javascript 从jQuery DataTable将行数据获取到数组中时出现问题,javascript,jquery,datatables,Javascript,Jquery,Datatables,请查看并告诉我为什么无法将所选行放入数组中? 他的代码是我用来获取数据的代码: $(document).ready(function () { var arrLoadDb = []; $('#example').DataTable(); $('.add-row-db').on('click', function (e) { var $this = $(this); $this.closest('tr').find('td:not(td:
$(document).ready(function () {
var arrLoadDb = [];
$('#example').DataTable();
$('.add-row-db').on('click', function (e) {
var $this = $(this);
$this.closest('tr').find('td:not(td:first, td:last)').each(function () {
arrLoadDb.push($(this).text());
console.log(arrLoadDb);
});
});
});
但我得到的是这个结果,而不是数组
看看这个
每次单击按钮时,它都会搜索除fist和last列之外的所有列值
对于每按一次按钮,我们需要清除数组,结果是一个数组。您的
arrLoadDb
变量是一个数组。您只需在循环外键入并获得预期的输出。另一个可能的基本错误是,每次单击都会将arrLoadDb
添加到,但从未清除,这看起来是错误的
$(document).ready(function() {
var arrLoadDb = [];
$('#example').DataTable();
$('.add-row-db').on('click', function(e){
var $this = $(this);
arrLoadDb = []
$this.closest('tr').find('td:not(td:first, td:last)').each(function(){
arrLoadDb.push($(this).text());
});
console.log(arrLoadDb);
});
} );