我正在尝试使用jQuery从表中捕获两个单元格的值。我的桌子看起来像这样:

    <table id="searchByLocationResults" class="table table-hover table-striped" cellspacing="0" style="border-collapse:collapse;">
        <tbody>
            <tr>
                <th scope="col">View Detail</th>
                <th scope="col">Quantity</th>
                <th scope="col" style="display:none;">Location ID</th>
                <th scope="col" style="display:none;">Item Type ID</th>
            </tr>
            @For Each item In Model
                Dim currentItem = item
                @<tr>
                     <td><a class="btn btn-default btn-sm" onclick="viewDetails(this)">View Detail</a></td>
                     <td>
                         @Html.DisplayFor(Function(modelItem) currentItem.COUNT)
                     </td>
                     <td style="display:none;" class="locationID">
                         @Html.DisplayFor(Function(modelItem) currentItem.Unique_Location_ID)
                     </td>
                     <td style="display:none;" class="itemType">
                         @Html.DisplayFor(Function(modelItem) currentItem.Item_Type_Identifier)
                     </td>
                </tr>
            Next
        </tbody>
    </table>


如您所见,每行上都有一个“查看详细信息”按钮。我需要做的是仅在单击按钮的行中单击按钮时用display:noneclass=locationID捕获单元格中的class=itemType值。我已经在堆栈上通过流herehere和许多其他解决方案看到了多种解决方案。其中大多数用于捕获整个值行。

我尝试了几种不同的脚本:

 function viewDetails(clickedRow) {
                var locationID = $(this).find(".selected td:.locationID").val();
                alert(locationID);


和:

function viewDetails(clickedRow) {
    var locationID = tr.find(".locationID").val();
    alert(locationID);


以及其他一些。

如何捕获单击“查看详细信息”的行的单元格locationIDitemType的值?

最佳答案

我会说尝试index。仅当按钮和文本区域具有唯一的类,因此计数正确时,此方法才有效。然后,一旦有了索引,就使用eq()来获取数据。

var index = $(".btn-sm").index();
var locationId = $(".locationID").eq(index).text();

08-08 07:17
查看更多