【问题标题】:Get column data when clicked on row with Razor and JS使用 Razor 和 JS 单击行时获取列数据
【发布时间】:2012-11-18 09:31:34
【问题描述】:

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

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

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

无论如何,我正在寻找的是在我点击的行上获得某一列。

剃刀 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>

【问题讨论】:

    标签: javascript jquery asp.net-mvc razor


    【解决方案1】:

    你可以使用nth-child

    Live Demo

    var table = $("#myTable tr");
    
    table.click(function () {
        alert($(':nth-child(1)',  this).text());
    });​
    

    【讨论】:

    • 嗯,这对我不起作用。所有警报显示为 :nth-child(1)。
    • 现在检查一下,我刚刚修好了。
    猜你喜欢
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 2022-10-26
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多