javascript - 如何从具有特定类名的最接近的 <td> 获取值?

标签 javascript jquery jeditable

我正在使用Jeditable在表内,更新值。

function updateOwner(row, value, settings) {

            var td = $(row).closest('.cvrnummer');

            alert(td.html());

            $.post('UserAdministration/UpdateOwners', { owner: value }, function (data) {
                alert(data);
            });
        }

        $('.editable').editable(function (value, settings) {
            updateOwner(this, value, settings);
            return (value);
        }, {
            data: " {'mona':'moma','cecilie':'cecilie','jacob':'jacob', 'morten':'morten', 'rasmus' : 'rasmus', 'selected':'mona'}",
            type: 'select',
            submit: 'OK'
        });

表格如下所示:

<table id="userTable" class="table-autofilter table-autosort" >
<thead>
<tr>
<th>CVR-Nummer</th>
<th>Firma navn</th>
<th class="table-filterable">Tilhører</th>
<th class="table-sortable:date">Oprettet</th>
<th>Sidst online</th>
<th></th>
<th class="table-sortable:numeric table-sortable">Besvaret (%)</th>
</tr>
</thead>
<tbody>
<tr>
       <td class="cvrnummer"></td>
       <td></td>
       <td class="editable" style="text-align: center;"></td>
       <td></td>   
       <td></td>
       <td>Se besvarelser</td>
       <td>
       </td>
</tr>
</tbody>
</table>

我想传递里面的值

<td class="cvrnummer"></td>

当我使用 AJAX 发布值时,但我不知道如何获取该值。我正在使用:

var td = $(row).closest('.cvrnummer');

            alert(td.html());

返回 null。

如何获取该值?

最佳答案

你应该这样做

var td = $(row).closest('tr').find('td.cvrnummer');

因为 closest() 在 DOM 树上向上,而 find() 在 DOM 树上向下。您不能使用 prev() 因为它只返回前一个元素

关于javascript - 如何从具有特定类名的最接近的 <td> 获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9414134/

相关文章:

javascript - 使用 JQuery 将内部 html 替换为 html 时间值

javascript - 在 React 中显示多个 API 响应

javascript - 是否可以使用 <a title ="<a>">

jquery - 使用 $.get 值作为 JSON 对象会返回 HierarchyRequestError : DOM Exception 3

jquery - jeditable(内联编辑器)不适用于动态生成的元素

javascript - 带有溢出自动/滚动的 jQuery scrollTop 动画

javascript - 使用 setInterval() 函数使图像闪烁

java - 验证 HTML 单元格中的数据并根据时间间隔显示消息

javascript - 动态重组 HTML 表格