Javascript 获取从MVC中的表中单击的行的Id

Javascript 获取从MVC中的表中单击的行的Id,javascript,jquery,asp.net-mvc,Javascript,Jquery,Asp.net Mvc,我想在用户单击按钮时获得所单击行的Id 我的代码如下 <table cellpadding="0" cellspacing="0" id="ProductGrid"> <tr> <th>P Id</th> <th>P Name</th> <th>Price</th> <th></th> <

我想在用户单击按钮时获得所单击行的
Id

我的代码如下

   <table cellpadding="0" cellspacing="0" id="ProductGrid">
    <tr>
        <th>P Id</th>
        <th>P Name</th>
        <th>Price</th>
        <th></th>
    </tr>
    @foreach(Mvc4API.linqtosql.tblProduct prod in Model)
    {
        <tr>
            <td>@prod.Pid</td>
            <td>@prod.Pname</td>
            <td>@prod.Price</td>
            <td><button type="button" class="ids" id="btnpartial" value="showdetail" onclick="getview()"></button></td>
        </tr>
    }
</table>
但它不起作用。

只需更新标签即可

button type="button" class="ids" id="btnpartial" value="showdetail" onclick="getview()"


它将在脚本中为您提供id。

像这样更改按钮标签

<td><button type="button" class="ids" id="btnpartial" value="showdetail" onclick="getview(@prod.Pid)"></button></td>
function getview(id) {
    debugger;
    var productId = id;
});

重复的id属性是无效的html。删除
id
属性和
onclick
并使用

$('.ids').click(function() {
    var productId = $(this).closest("tr").find("td").eq(0).text();
});
或者,将值添加到
数据
属性中

<button type="button" class="ids" data-id="@prod.Pid"></button>
请注意,当前选择器-
$(“#ProductGrid.ids”)
返回所有按钮元素,而不是您单击的按钮元素。

请尝试以下代码

查看

<td><button type="button" class="ids" id="btnpartial" value="showdetail" onclick="getview(@prod.Pid)"></button></td>
<button type="button" class="ids" data-id="@prod.Pid"></button>
$('.ids').click(function() {
    var productId = $(this).data('id');
});
<td><button type="button" class="ids" id="btnpartial" value="showdetail" onclick="getview(@prod.Pid)"></button></td>
function getview(x)
{
   var id = x; //Id will get here.
}