2013-08-29 89 views
0

我有一个包含搜索字段的表格。搜索数据后,列出的数据将被导出到excel中,这需要添加一些额外的信息,这些信息未在表中显示,需要从数据库中提取。 所以,我需要访问所有列出的数据的id(在seraching之后)。此外,该标签没有相关的'id'或'class',因此它可以被抓取,因为它是使用数据表格呈现的。这怎么可能使用jQuery从表头中获取列元素

代码片段

<table id='mytable' border='1'> 
    <tr> 
     <th id='ID'>ID</th> 
     <th id='Email'>Email</th> 
    </tr> 
    <tr> 
     <td>1</td> 
     <td>[email protected]</td> 
    </tr> 
    <tr> 
     <td>2</td> 
     <td>[email protected]</td> 
    </tr> 
    <tr> 
     <td>3</td> 
     <td>[email protected]</td> 
    </tr> 
</table> 

<button id='gen'>Generate excel file</button> 

实现的jsfiddle:http://jsfiddle.net/xLA3g/2/

回答

1

看看这个,可能有助于 http://jsfiddle.net/xLA3g/3/

$('#mytable tr th').each(function(i){ 
    alert($(this).text()); 
}); 

答案更新,显示td列号 http://jsfiddle.net/xLA3g/4/

$('#mytable tr td:first-child').each(function(i){ 
    alert($(this).text()); 
}); 

让我知道如果我能帮助协助任何进一步

+0

感谢....这只是仅表现为表头。我试图在这个例子中提取像1,2,3这样的ID – beebek

+0

是的,第二个答案是解决方案。非常感谢@caramba – beebek

+0

我已经在jsfiddle中发布了完整的解决方案供其他人使用。 http://jsfiddle.net/keJBZ/3/ – beebek

1

修改一点点@carambas回答

var ids=[]; 
$('#mytable tr th').each(function(i){ 
    alert($(this).attr('id')); 
    ids.push($(this).attr('id')); 

}); 
console.log("ID",ids);