简体   繁体   English

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

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

i'm having a problem with clicking a button inside html table and get the value on the same row and pass it to an input FullName . 我在单击html表中的按钮时遇到问题,并在同一行上获取值并将其传递给输入FullName。 how can i do that? 我怎样才能做到这一点? thanks 谢谢

<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" >

You can use like this, 你可以这样使用,

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

Closest("tr") will return the clicked button's parent tr. Closest("tr")将返回单击按钮的父tr。

Then you can find the child td using its index. 然后你可以使用它的索引找到孩子td。

find("td:eq(0)") will return the first td of selected tr . find("td:eq(0)")将返回所选tr的第一个td In your case, it will return signatoryname . 在您的情况下,它将返回signatoryname

Likewise, you can use find("td:eq(1)") & find("td:eq(2)") 同样,你可以使用find("td:eq(1)")find("td:eq(2)")

you can try this, or change your structure as per you hierarchy . 您可以尝试这个,或根据您的层次结构更改您的结构。

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

working fiddle 工作小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM