简体   繁体   English

获取当前表行的值

[英]Get the value of current table row

So I have multiple <tr> elements and I need to find the Id which corresponds to that object. 所以我有多个<tr>元素,我需要找到对应于该对象的ID。 Basically when I click edit I need the selected ID to be alerted to the user. 基本上,当我单击“编辑”时,我需要将选定的ID通知用户。

在此处输入图片说明

    <tr>
        <td ><input type="hidden" name="pkAdminId" value="1076">1076</td>
        <td><a class="editCredential" class="openWindow" data-modal-id="existingCredential" data-credential-id="1076">Edit</a></td>
        <td><a class="delete">Delete</a></td>
    </tr>
    <tr>
        <td ><input type="hidden" name="pkAdminId" value="1073">1073</td>
        <td><a class="editCredential" class="openWindow" data-modal-id="existingCredential" data-credential-id="1073">Edit</a></td>
        <td><a class="delete">Delete</a></td>
    </tr>
    <tr>
        <td ><input type="hidden" name="pkAdminId" value="1074">1074</td>
        <td><a class="editCredential" class="openWindow" data-modal-id="existingCredential" data-credential-id="1074">Edit</a></td>
        <td><a class="delete">Delete</a></td>
    </tr>
    <tr>
        <td ><input type="hidden" name="pkAdminId" value="1075">1075</td>
        <td><a class="editCredential" class="openWindow" data-modal-id="existingCredential" data-credential-id="1075">Edit</a></td>
        <td><a class="delete">Delete</a></td>
    </tr>

The JQuery which I have tried only finds the value of the first table row which is 1076 in this case. 我尝试过的JQuery在这种情况下只能找到第一个表行的值,即1076。

$('.existingCredential').on('click', '.delete', function() {
    var id = $('input[name="pkAdminId"]').val();
    alert(id);
});

You can use closest along with element context this to traverse to parent tr and then find element in it: 您可以使用closest和元素上下文this遍历到父级tr,然后在其中查找元素:

$('.existingCredential').on('click', '.delete', function() {
 var id = $(this).closest('tr').find('input[name="pkAdminId"]').val();
 alert(id);
});

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

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