在jquery中将表的行检查为数组th作为数组索引
我试图使用jQuery将HTML表的选中行仅推送到数组中。我需要将数组索引作为表头,将值作为相应头的选中列shell的shell值 我的HTML表格如下所示:在jquery中将表的行检查为数组th作为数组索引,jquery,html,Jquery,Html,我试图使用jQuery将HTML表的选中行仅推送到数组中。我需要将数组索引作为表头,将值作为相应头的选中列shell的shell值 我的HTML表格如下所示: <table class="table table-bordered" id="checkTable"> <thead> <th style="width:20px;"><input type="checkbox" name="headercheck"
<table class="table table-bordered" id="checkTable">
<thead>
<th style="width:20px;"><input type="checkbox" name="headercheck" id="checkAll"></th>
<th>Programming Languages</th>
<th>Popularity</th>
</thead>
<tbody>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td>Java</td>
<td>62%</td>
</tr>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td>Python</td>
<td>46%</td>
</tr>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td>JavaScript</td>
<td>38%</td>
</tr>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td>C++</td>
<td>31%</td>
</tr>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td>C#</td>
<td>27%</td>
</tr>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td>PHP</td>
<td>14%</td>
</tr>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td>Pearl</td>
<td>14%</td>
</tr>
</tbody>
</table>
<button id='pushToArray'>Push To Array</button>
我成功地构建了头文件,但在索引时,我将数组作为
[…]
0: []
"Programming Languages": "14%"
length: 0
undefined: "PHP"
__proto__: Array []
length: 1
__proto__: Array []
什么是让事情按照我希望的方式正确进行的最佳方式
$('div#tableContainer')。查找('button#pushToArray')。打开('click',function()){
var数组=新数组();
var headers=新数组();
表.查找('th')。每个函数(索引,项){
如果(索引==0){
返回true;
}否则{
标题[索引]=$(项).html();
}
});
var valueList=[];
table.find('tbody tr')。每个(函数(){
$(this).find('input.table checkbox:checked')。每个(函数(){
var值=[];
$(this).最近的('td')。兄弟('td')。每个(函数(索引,项){
值[标题[索引]]=$(项).html();
});
值列表。推送(值);
});
});
控制台日志(valueList);
});代码>
程序设计语言
人气
JAVA
62%
python
46%
JavaScript
38%
C++
31%
C#
27%
PHP
14%
珍珠
14%
按数组
如果要选中编程语言,可以使用:checked
选择器选中全部复选框。使用映射
获取下一个
$('pushToArray')。单击(函数(){
//获取所有不包含checkAll复选框的标题
var headers=$(“#checkTable thead th”).filter(函数(){
return!$(this).find('#checkAll').length;
}).map(函数(){
返回$(this.text().trim();
}).get();
//循环通过选中的复选框
var arr=$('#checkTable.table checkbox:checked').map(函数(){
var obj={};
$(this).parent().sides().each(函数(i){
obj[headers[i]=$(this.text().trim();
})
返回obj;
}).get();
控制台日志(arr);
});代码>
程序设计语言
人气
JAVA
62%
python
46%
JavaScript
38%
C++
31%
C#
27%
PHP
14%
珍珠
14%
Push To Array
我使用循环来推送数组中的标题,并使用另一个循环来获取表体值,忽略复选框,如下所示:
$('div#tableContainer').find('button#pushToArray').on('click', function(){
console.log("Cool");
var array = [];
var headers = [];
$('#checkTable th').each(function(index, item) {
if(index == 0)
{
return true;
}
else
{
headers[index] = $(item).html();
}
});
$('#checkTable tr').has('td input.table-checkbox:checked').each(function() {
var arrayItem = {};
$('td', $(this)).each(function(index, item) {
if(index == 0)
{
return true;
}
else
{
arrayItem[headers[index]] = $(item).html();
}
});
array.push(arrayItem);
});
console.log(array);
});
这个'div#tableContainer'
,table
变量在哪里??请将按钮张贴在id为TableContainer的div中,您希望得到什么输出@AleshI只需要一个以表头作为索引的数组,它的值必须是每个检查行的值。但我需要表头作为索引[…] 0: […] “编程语言”:“Python” “受欢迎程度”:“46%”是的,但它不能与列保持静态。你的意思是,列名称是动态的?位置也是动态的吗?您这样做的方式只给出了两列值,但是如果我的表中有两列以上的值呢
$('div#tableContainer').find('button#pushToArray').on('click', function(){
console.log("Cool");
var array = [];
var headers = [];
$('#checkTable th').each(function(index, item) {
if(index == 0)
{
return true;
}
else
{
headers[index] = $(item).html();
}
});
$('#checkTable tr').has('td input.table-checkbox:checked').each(function() {
var arrayItem = {};
$('td', $(this)).each(function(index, item) {
if(index == 0)
{
return true;
}
else
{
arrayItem[headers[index]] = $(item).html();
}
});
array.push(arrayItem);
});
console.log(array);
});