gpt4 book ai didi

javascript - 使用 Razor 和 JS 单击行时获取列数据

转载 作者:行者123 更新时间:2023-11-28 02:36:19 25 4
gpt4 key购买 nike

我有一个在 MVC 中使用 Razor 动态构建的表。

我希望能够单击一行并获取第一列 ID 的值。

现在它正在获取所有列,但我不太了解 Javascript 或它如何与文本一起存储。

无论如何,我正在寻找的是获取我单击的行上的特定列。

Razor MVC

<table id="myTable" class="tablesorter"> 
<thead>
<tr>
<th>ID</th>
<th>Owner</th>
</tr>
</thead>
<tbody>
@for(int i=0; i<Model.changes.Count(); i++)
{
<tr>
<td>@Model.changes[i].ID</td>
<td>@Model.changes[i].Owner</td>
</tr>
}
</tbody>
</table>

Javascript

<script>

var table = $("#myTable tr");

table.click(function () {
alert($(this).text());
});

</script>

最佳答案

您可以使用nth-child ,

<强> Live Demo

var table = $("#myTable tr");

table.click(function () {
alert($(':nth-child(1)', this).text());
});​

关于javascript - 使用 Razor 和 JS 单击行时获取列数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13436592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com