Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从<;tr>;Jquery数据表中的标记_Javascript_Jquery_Datatables - Fatal编程技术网

Javascript 从<;tr>;Jquery数据表中的标记

Javascript 从<;tr>;Jquery数据表中的标记,javascript,jquery,datatables,Javascript,Jquery,Datatables,我试图用数据表创建一个表,在选择一行之后,有一个按钮将用户带到一个包含更多详细信息的新页面 在这里完成教程之后 我正在考虑将设置为一个唯一的标识符,在选择行然后按下按钮时,它将传递id并获得更详细的信息 然而,我很难得到这个id字段,我正在寻找类似的东西 $('#detailButton').click(function(){ window.location = 'detail.php?id=' + table.row('.selected').id; }); 编辑: 很抱歉澄

我试图用数据表创建一个表,在选择一行之后,有一个按钮将用户带到一个包含更多详细信息的新页面

在这里完成教程之后

我正在考虑将
设置为一个唯一的标识符,在选择行然后按下按钮时,它将传递id并获得更详细的信息

然而,我很难得到这个id字段,我正在寻找类似的东西

  $('#detailButton').click(function(){
    window.location = 'detail.php?id=' + table.row('.selected').id;
  });
编辑: 很抱歉澄清,我只想在页面底部有一个按钮,不是每行一个按钮,也不是单击该行。
在本教程中,当选中该行时,它会将类设置为“selected”,因此我希望获得选中该类的行的id。

我假设每行都有一个按钮,在这种情况下,每个按钮都需要有一个唯一的id,该id需要以某种方式引用该行。 另一个选项是获取表中的按钮偏移量,从而确定按钮位于哪一行,例如:button.parentNode可以为您提供按钮所在的行。但我不确定你的html是什么样子

使用
$(this)
jQuery元素可以获得触发
事件的元素。如果您有一个在选择行时激发的事件,则可以获取元素并查找id

HTML:


我要找的其实只是attr(“id”)的部分,谢谢
<table style="width:300px">
    <tr id="row0">
        <td>Jill</td>
        <td>Smith</td> 
        <td>50</td>
        <td>
            <button class="rowButton">select</button>
        </td>
    </tr>
    <tr id="row1">
        <td>Eve</td>
        <td>Jackson</td> 
        <td>94</td>
        <td>
            <button class="rowButton">select</button>
        </td>
    </tr>
</table>
$('.rowButton').on('click', function(){
    window.location = 'detail.php?id=' + $(this).closest('tr').attr('id'); // read id from tr element
});
$('table tr').on('click', 'button', function(){
    window.location = 'detail.php?id=' + $(this).parents('tr').attr('id'); // read id from tr element
});