jquery - 如何找到最近的表 jQuery

标签 jquery html-table keyup closest

当我搜索值时,我试图获取最近的表。

这是我的代码,但我总是得到“未定义”。

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/

相关文章:

javascript - 用户 Modernizr 强制浏览器使用 jQuery UI datepicker 而不是浏览器 datepicker?

javascript - 设置全局基本 URL

html - 在 IE8 中显示背景图像

javascript .keyup 正确的价格格式等等

javascript - 将 JavaScript 添加到 w2ui 面板

javascript - Jquery ajax 与 XMLHttpRequest

html - HTML 和 CSS 中带有底线的图书索引布局样式

php - 很难从两个表中获取数据

javascript - 简单的 jQuery 页面内容搜索显示不准确的结果

jQuery - 通过 KeyUp/KeyDown 滚动到下一个/上一个部分