当我搜索值时,我试图获取最近的表。
这是我的代码,但我总是得到“未定义”。
jQuery:
$(function() {
$("#searchValue").keyup(function() {
alert($(this).closest('table').attr('id'));
});
});
HTML:
<input type='text' name='searchValue' id='searchValue'/>
<table id='tablePlanning' class='tablesorter'>
<thead>
<tr>
<th>PR Code</th>
<th>Klant</th>
<th>Description</th>
<th>Project status</th>
<th>Project Leader</th>
<th>Coordinator</th>
<th>Account manager</th>
<th>Billing</th>
<th>Start Datum</th>
<th>Hardware</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
最佳答案
尝试使用 .next()
查找以下表
(在同一 DOM 级别):
$(function() {
$("#searchValue").keyup(function() {
alert($(this).next('table').attr('id'));
});
});
.closest()
是查找元素的第一个父元素。
但是由于您的表有一个 id
,您应该使用它(我希望您没有多个具有 id #tablePlanning
的表,否则,您应该使用类名。
关于jquery - 如何找到最近的表 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20475208/