【发布时间】:2016-06-16 15:09:10
【问题描述】:
我有一个表,表头有两行,第一行有 colspan,第二行有第二行标题,见下图
我有一个 jquery 函数,它从第二个标题行中获取所有表格单元格值,并将其添加到一个带有复选框的 div 以及稍后用于切换的索引号。 1st Column, 2nd Column 和 3rd Column 去分开div的命名speed, cons, dest
现在切换工作正常,它检查列的索引号并添加隐藏,因此 colspan 减少以匹配表格格式。
但是,每当单击 2nd Column 和 3rd Column 的第一个复选框时,表格会中断,我试图找出问题,但无法使其正常工作。有什么建议吗?
这里是fiddle请推荐
下面是其中一个 div 的过滤器切换
$('.dest input[type=checkbox]').click(function() {
var index = $(this).attr('index');
$('.table thead .this_h th').eq(index).toggleClass('hidden');
var hidden = $('.table thead th.hidden')
$.each(hidden, function() {
var idx = $(this).index() + 1;
$.each($('.table tbody tr'), function() {
$(this).find('td').eq(idx).hide();
});
});
var visible = $('.table thead .this_h th:not(.hidden)');
$.each(visible, function() {
var idx = $(this).index() + 1;
$.each($('.table tbody tr'), function() {
$(this).find('td').eq(idx).show();
});
});
var length = 0;
var temp_name = '';
$(".table thead tr:nth-child(2)").find('th').each(function(e, a) {
if (temp_name == $(a).attr('name')) {
console.log($(a).attr('name'));
if ($(a).is(':visible')) {
length = length + 1;
}
}
else
{
console.log(temp_name);
$(".table thead tr:nth-child(1)").find("th[name=" + temp_name + "]").attr('colspan', length);
if ($(a).is(':visible')) {
length = 1;
temp_name = $(a).attr('name');
} else {
length = 0;
}
}
})
});
【问题讨论】:
-
请查看我更新的答案。
标签: javascript jquery arrays html html-table