Get table id by click in td using jQuery

前端 未结 4 1370
不知归路
不知归路 2021-01-12 17:06

I have a tr in a table header that contains several input fields.

Is there a way that by click on one of these input fields I can get the id of the parent table ?

相关标签:
4条回答
  • 2021-01-12 18:04
    $("#tableID").on("click","input[type='text']",function(){
    
    $(this).closest('table').attr('id');
    
    });
    

    reference closest()

    0 讨论(0)
  • 2021-01-12 18:04

    You can use

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

    it works.

    0 讨论(0)
  • 2021-01-12 18:08

    In the click handler you can use .closest()

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

    0 讨论(0)
  • 2021-01-12 18:08

    You should delegate event to avoid multiple handlers, and then use delegateTarget to target current TABLE:

    $('table').on('click', 'input', function (e) {
        alert(e.delegateTarget.id);
    });
    
    0 讨论(0)
提交回复
热议问题