只需单击即可获取表行数据

sni*_*ggy 8 jquery html-table

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

例如,在我看来,我有一个这样的表.

      <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>
Run Code Online (Sandbox Code Playgroud)

当我运行项目时,我会得到这样的东西:

在此输入图像描述

如果用户单击列客户端名称:BBB.它将有一个弹出窗口说:您好,您已选择客户端ID:002,客户端名称:BBB,客户端添加:xxxxx.

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

这该怎么做?请帮忙.

HTML:@model IEnumerable @ {ViewBag.Title ="Client"; Layout ="〜/ 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>
Run Code Online (Sandbox Code Playgroud)

tym*_*eJV 12

你可以这样做:

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

    console.log(tableData);
});
Run Code Online (Sandbox Code Playgroud)

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

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