参见英文答案 >
Get the contents of a table row with a button click6个
我有一个表,我想让用户点击表格上的任何元素来获取数据行.我怎么做?
我有一个表,我想让用户点击表格上的任何元素来获取数据行.我怎么做?
例如,在我看来,我有一个这样的表.
- <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>
当我运行项目时,我会得到这样的东西:
如果用户单击列客户端名称:BBB.它将有一个弹出窗口说:您好,您已选择客户ID:002,客户名称:BBB,客户端添加:xxxxx.
用户可以单击所有列,它仍将在弹出窗口中返回整行数据.
这个怎么做?请帮忙.
HTML:
@model IEnumerable
@ {
ViewBag.Title =“客户”;
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>
解决方法
你可以这样做:
- $("tr.myTable").click(function() {
- var tableData = $(this).children("td").map(function() {
- return $(this).text();
- }).get();
- console.log(tableData);
- });
返回一个很好的数据数组,然后你可以按照你想要的方式显示它.