检测该行并使用javascript获取td值.(动态表)

Detect the row and get the td value using javascript. (Dynamic table)

本文关键字:td 动态 获取 javascript 检测      更新时间:2023-09-26

我完全想避免使用内联javascript!但是我有一个表,它是使用php从mysql数据库动态生成的。我希望,如果用户单击任何一行,它会创建一个包含有关单击行信息的模态。现在我可以初始化模态了,但是表的第一列包含一个索引值,可以用来检索该行的信息。我的问题是如何使用javascript检索索引值。要启动模式,我使用以下代码:

$('#production tr').click(function(){
     $('#review_order').modal('show');
}) ;    

表格标记:

<table id="order_basket" class="table table-bordered">
    <thead>
       <th>Voucher ID</th>
       <th>Title</th>
       <th>Created By</th>
    </thead>
    <tbody>
       <tr><td>1<td>
        <td>Something here</td>
        <td>Some Person</td></tr>
    </tbody>
<table>

除非添加了rowspan/colspan属性,否则一行中的第一列是tr的第一个子td

$('#production tr').click(function() {
    var index = $(this).children("td").first().text();
    // `index` is the text in the first column in the clicked row
    $('#review_order').modal('show');
});