我想在搜索值时获取最接近的表格。
这是我的代码,但我总是得到“undefined”。
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>
closest
,您的表格应该是 #searchValue 的一个parent
。 - Ricardo Binns$("#searchValue")
位于一个<table>
元素内部,我唯一能想到的解释是,该表格没有设置id
属性。 - Barney.next('table')
或siblings('table')
或$('#tablePlanning')
? - putvandeclosest()
并不是一个迭代祖先的最合适的方法名称... - Frédéric Hamidi