首页 > 解决方案 > 通过行点击事件传递动态创建的 html 表格行内文 ID

问题描述

我有一个htmltable动态创建的。我已使行可点击。我需要将行内部文本 id 传递给单击该行时触发的脚本。

htmltable

<table style="width:100%;">
     <tr>
        <th>id</th>
        <th>name</th>
        <th>other info</th>
     </tr>
    <tr>
         <td class='table_row_click'>11</td>
        <td class='table_row_click'>item 2</td>
        <td class='table_row_click'>lmfao</td>
    </tr>
    <tr>
        <td class='table_row_click'>22</td>
        <td class='table_row_click'>item 2</td>
        <td class='table_row_click'>lol</td>
    </tr>
</table>

点击事件:

$(document).ready(function ($) {
           $(".table_row_click").click(function (e) {

//I need to use the clicked row ID here for something 

            });
       });

标签: javascriptjqueryasp.nethtml-tableclick

解决方案


$(e.target).closest('tr').find('td').first().text();

您可以向上导航到父 tr,找到 td 元素,获取第一个元素,然后获取其文本。

closest('tr')如果类在 td 或 tr 级别,则using将起作用,因为closest()可以匹配自身。


推荐阅读