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

使用jQuery通过单击td获取表id,jquery,identifier,Jquery,Identifier,我在包含几个输入字段的表头中有一个tr 是否有一种方法可以通过单击其中一个输入字段来获取父表的id 我的tr看起来像这样: <table id="tableID"> <thead> <tr> <th><input type="text" name="input1" id="input1" /></th> <th><input type=

我在包含几个输入字段的表头中有一个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>

// ...

在单击处理程序中,您可以使用


$(this).closest('table').attr('id')
您应该委派事件以避免多个处理程序,然后使用
delegateTarget
以当前表为目标:

$('table').on('click', 'input', function (e) {
    alert(e.delegateTarget.id);
});
参考

您可以使用

$('#tdId').click(function(){
    $(this).parents('table').attr('id');
});

它可以工作。

@Arun p Johny非常快,特别是Jquery!!这正是我错过的。这是完美的-非常感谢您的快速回复!您可以使用$(this.parents('table').attr('id');
$('#tdId').click(function(){
    $(this).parents('table').attr('id');
});