JavaScript - Get data of first cell of selected table row

后端 未结 3 1034
挽巷
挽巷 2021-01-26 22:37
相关标签:
3条回答
  • 2021-01-26 22:49
    $(".mybutton").click(function(){
        alert($(this).parent().siblings().eq(0).text());
    });
    
    0 讨论(0)
  • 2021-01-26 22:56

    You can use something like

    Demo

    $(window).on('click', '.mybutton' ,function() {
        alert($(this).parent().parent().find('.nr').text());
        //Or you can use, which is even better
        //alert($(this).parent().siblings('.nr').text());
    });
    

    Here, the selector is pretty simple, we are first binding click event on the button, and onclick we select the button element parent i.e td and we select the parent of td i.e tr and later we find an element with a class of .nr

    You can also write td.nr instead of just .nr to be more specific.

    Your rows are being dynamically added, in order for your click listener to work you will have to delegate the events

    Credits to @Patsy Issa for suggesting .siblings()

    0 讨论(0)
  • 2021-01-26 23:13

    usually im using DataTables js for solution, you can check at: https://datatables.net/reference/api/row().data()

    0 讨论(0)
提交回复
热议问题
Room No