单击 HTML 表中的按钮并获取同一行上的值


click a button inside html table and get the value on the same row

我在单击 html 表中的按钮并获取同一行的值并将其传递给输入全名时遇到问题.我该怎么做?谢谢

<td><?php echo $r['signatoryname'] ?></td>
                                <td><?php echo $r['signatoryposition'] ?></td>
                                <td><?php echo $r['signatoryoffice'] ?></td>
                                <td>
                                <button type="button" class="btnedit btn btn-xs btn-success">
                                <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>Edit
                                </button>
                                </td>
<input id="Fullname" class="form-control" >

你可以这样使用,

$(".btnedit").click(function () {
    var name = $(this).closest("tr").find("td:eq(0)").text();
    $("#Fullname").val(name);
});

Closest("tr")将返回单击按钮的父 tr。

然后,您可以使用其索引找到子 td。

find("td:eq(0)")将返回所选tr的第一个td。在您的情况下,它将返回签名者名称

同样,您可以使用find("td:eq(1)")find("td:eq(2)")

您可以尝试此操作,也可以根据您的层次结构更改结构。

$('.btnedit').click(
 function(){
  var uName = $(this).parent().prev().html();
  $(this).parent().next().children('.form-control').val(uName)
 });

工作小提琴