【问题标题】:How to post the value of a specific cell of a table using jQuery如何使用jQuery发布表格的特定单元格的值
【发布时间】:2015-07-28 22:38:43
【问题描述】:

我正在尝试使用 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>

如您所见,每一行都有一个“查看详细信息”按钮。我需要做的是在单击按钮时使用class=locationIDclass=itemType 捕获单元格中的display:none 值,仅针对单击按钮的行。我已经在堆栈上看到了多种解决方案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 的值?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    我会说试试index。这仅适用于按钮和文本区域具有唯一类以便计数正常工作的情况。然后,一旦你有了索引,就使用eq() 来获取数据。

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

    【讨论】:

      【解决方案2】:

      你快到了。你只需要在Jquery中使用.text()方法

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

      【讨论】:

      • 这不起作用,我从来没有触发过警报。不过,我正在使用该功能,并在变量分配之前通过警报进行了验证。我已经检查了四次拼写。
      • 对不起。我假设您的 tr 已经在相关行获得。
      猜你喜欢
      • 1970-01-01
      • 2011-08-02
      • 1970-01-01
      • 2011-03-19
      • 2010-09-27
      • 2011-08-03
      • 2017-01-24
      • 2019-02-09
      • 1970-01-01
      相关资源
      最近更新 更多