简体   繁体   English

jQuery:确定从哪个 <tr> 该函数称为

[英]jQuery: Determine from which <tr> the function is called

I have a knockout grid that looks like this (nothing special): 我有一个看起来像这样的淘汰赛网格(没什么特别的):

Knockout.js网格可编辑

When I click Edit, it must determine in which <tr> it was called. 当我单击“编辑”时,它必须确定在哪个<tr>中调用它。 For Example I click edit on the first user, it must log in console "1", and so on. 例如,我在第一个用户上单击“编辑”,它必须登录控制台“ 1”,依此类推。

My table's code: 我表的代码:

<table class="table table-striped table-bordered table-condensed" data-bind='visible: users().length > 0'>
    <thead>
        <tr>
            <th>UserId</th>
            <th>Username</th>
            <th>FirstName</th>
            <th>LastName</th>
            <th>Email</th>
            <th>Date</th>
            <th />
        </tr>
    </thead>
    <tbody data-bind='foreach: users'>
        <tr>
            <td>
                <label data-bind="text: UserId" />
            </td>
            <td>
                <input data-bind='value: Username' />
                <label data-bind="text: Username" />
            </td>
            <td>
                <input data-bind='value: FirstName' />
                <label data-bind="text: FirstName" />
            </td>
            <td>
                <input data-bind='value: LastName' />
                <label data-bind="text: LastName" />
            </td>
            <td>
                <input data-bind='value: Email' />
                <label data-bind="text: Email" />
            </td>
            <td>
                <input data-bind='value: Date' />
                <label data-bind="text: Date" />
            </td>
            <td class="tools">
                <a class="Apply" href="#" data-bind="click: $root.applyUser">Apply</a>
                <a class="Edit" href="#" data-bind="click: $root.editUser">Edit</a>
                <a class="Update" href="#" data-bind='click: $root.updateUser'>Update</a>
                <a class="Delete" href='#' data-bind='click: $root.removeUser'>Delete</a>
            </td>
        </tr>
    </tbody>
</table>

This is my attempt: 这是我的尝试:

$('a.Edit').click(function () {
                    var rowIndex = $(this).parent()
                                          .parent()
                                          .children()
                                          .index($(this).parent());
                    console.log('Row: ' + rowIndex);
                });

What am I missing? 我想念什么?

You can rather use .closest() for traversing to closest tr parent. 您可以使用.closest()遍历到最接近的tr父级。

IF tr elements are not siblings of each other: pass that object to method .index() on tr collection: 如果tr元素不是彼此的兄弟姐妹:将该对象传递给tr集合上的方法.index()

 $('tr').index($(this).closest('tr'));

IF tr elements are siblings of each other: 如果tr元素是彼此的兄弟姐妹:

 $(this).closest("tr").index()

I would have done this as below- 我本来会这样做的-

By giving a class to parent tr- 通过给父级tr-

<tr class="parent">

And then- 接着-

$(this).closest('tr.parent').index()

Because if the case is nested tr(s) then parent() wont' be working for longer. 因为如果案例是嵌套的tr(s)parent()将不再工作更长的时间。

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

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