JavaScript - Get data of first cell of selected table row

后端 未结 3 1036
挽巷
挽巷 2021-01-26 22:37
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    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()

提交回复
热议问题
Room No