0
我有一个表,看起来像这样 - >如何抓住数据在任何表行(HTML/JavaScript)的使用树遍历方法
<table>
<tr style="display: none";><td class="index">index_value</td></tr>
<tr><td>section header</td></tr>
<tr>
<td class="name>Steven</td>
<td class="height">6 ft.</td>
<td><button class="add">Add</button></td>
</tr>
</table
和JS脚本,看起来像这样 - >
<script>
$(".add").on('click', function(){
var the_name = $(this).closest("td").siblings(".name").text();
var the_type = $(this).closest("td").siblings(".type").text();
var the_index = $(this).parent().find("td.index").text();
}
</script>
正如你可以告诉,我试图获得在这个表内的某些td的值。前两个变量工作得很好,因为它们在同一个表格行内;但是,最后一个变量没有捕获我想要的数据(在索引类中)。
我在理解这些遍历树的方法时遇到了一些麻烦,以及如何在包含单击按钮的表行中获取数据。任何意见将不胜感激,谢谢。
巴比SPEIRS指出的那样,你的'$(本).parent()...'的使用目标的'td'封闭的按钮。如果你仍然想使用'parent()',那么你需要再次增加两次,比如'$(this).parent()。parent()。parent()。find(...)... ' – mrogers