jquery - 点击获取表格行数据

标签 jquery html-table

我有一个表格,我想允许用户单击表格上的任何元素来获取数据行。我该怎么做?

例如,我的 View 中有一个这样的表。

      <table class="myTable">
      :     <tr class="table">
                <th class="table">
                    Client ID
                </th>
                <th class="table">
                    Client Name
                </th>
                <th class="table">
                    Client Address
                </th>
            </tr>

当我运行该项目时,我会得到如下信息:

enter image description here

如果用户单击“客户端名称:BBB”列。它将弹出一个窗口,显示:嗨,您已选择客户端 ID:002,客户端名称:BBB,客户端地址:xxxxx。

用户可以点击所有列,仍然会在弹出窗口中返回整行数据。

如何做到这一点?请帮忙。

HTML: @model IEnumerable @{ ViewBag.Title = "客户端"; 布局=“~/Views/Shared/_Layout.cshtml”; }

<div class="body">

<div class="outer">
    <div class="inner">
        <table class="myTable">
            <tr class="table">
                <th class="table">
                    Client Name
                </th>
                <th class="table">
                    Client Code
                </th>
                <th class="table">
                    Client Address
                </th>
                <th class="searchTable">
                    Client Lead Partner
                </th>
            </tr>
            @foreach (var i in Model)
            {
                <tr class="myTable">
                    <td class="table">@i.ClientName
                    </td>
                    <td class="table">@i.ClientCode
                    </td>
                    <td class="table">@i.ClientAddress
                    </td>
                </tr>
            }
        </table>
    </div>
</div>

最佳答案

你可以这样做:

$("tr.myTable").click(function() {
    var tableData = $(this).children("td").map(function() {
        return $(this).text();
    }).get();

    console.log(tableData);
});

返回一个很好的数据数组,然后你可以按照你想要的方式显示它。

演示:http://jsfiddle.net/Sc5N7/

关于jquery - 点击获取表格行数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17356497/

相关文章:

javascript - 重新排列 javascript 对象 - 使用 .sort() 的键不会保留在正在排序的数组上

php - jQuery .live 多次触发!

jQuery tablesorter 插件 - 禁用标题内图像的排序

用于切换表格单元格可见性的 JavaScript

javascript - 警报不显示在 JSON 回调中

jquery - 将一个表中的所有内容复制到另一个充满输入框的表中

javascript - 在 div 和 mouseoverstate 中保留 radio 标签以用于 radio

html - 表格宽度在移动设备上奇怪地缩小

html - Firefox "bleeding"在 tr 上出现边界 - 错误?

html - 无法访问html表中的数组元素