jquery - 使用 jQuery 通过单击 td 获取表 id

标签 jquery identifier

我的表头中有一个 tr,其中包含多个输入字段。

有没有一种方法可以通过单击这些输入字段之一来获取父表的 ID?

我的 tr 看起来像这样:

<table id="tableID">
    <thead>
        <tr>
            <th><input type="text" name="input1" id="input1" /></th>
            <th><input type="text" name="input2" id="input2" /></th>
            <th><input type="text" name="input3" id="input3" /></th>
        </tr>
    </thead>
    <tbody>
        // ...
    </tbody>
</table>

最佳答案

在点击处理程序中,您可以使用 .closest()

$(this).closest('table').attr('id')

关于jquery - 使用 jQuery 通过单击 td 获取表 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20175970/

相关文章:

php - CLI Doctrine 2 - 索引已经定义

仅限 C# 变量名称 "_"(下划线)

javascript - 让 Ajax 为 Wordpress 上的所有用户工作

javascript - 通过 jquery setTimeout 更改 css 背景图像属性

PHP/Javascript/JQuery - base64 sha256 编码

javascript - 如何使 Bootstrap 中 col 内的元素比 col 宽?

ios - 自定义表格单元格类在 cellForRowAt 中不起作用

jqGrid 插件 - 没有自动行 ID?

Android 10 : IMEI no longer available on API 29. 寻找替代品

jquery根据给定的数字添加多个表列