[英]Get Id of the row clicked from the table in MVC
I want to get Id
of row clicked when a user clicks a button. 我想在用户单击按钮时单击行
Id
。
My code is as below. 我的代码如下。
<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>
I tried:- 我试过了:-
function getview() {
debugger;
var productId = $("#ProductGrid .ids").closest("tr").find("td").eq(0).html();
});
But it is not working. 但这是行不通的。
Duplicate id attributes are invalid html. 重复的id属性是无效的html。 Remove the
id
attribute and the onclick
and use 删除
id
属性和onclick
并使用
$('.ids').click(function() {
var productId = $(this).closest("tr").find("td").eq(0).text();
});
Alternatively, add the value in a data
attribute 或者,将值添加到
data
属性中
<button type="button" class="ids" data-id="@prod.Pid"></button>
and use 和使用
$('.ids').click(function() {
var productId = $(this).data('id');
});
Note that your current selector - $("#ProductGrid .ids")
returns all the button elements, not the one you clicked. 请注意,当前选择器-
$("#ProductGrid .ids")
返回所有按钮元素,而不是您单击的按钮元素。
Just update the tag. 只需更新标签即可。
button type="button" class="ids" id="btnpartial" value="showdetail" onclick="getview()"
to 至
button type="button" class="ids" id="btnpartial" value="showdetail" onclick="getview(this)"
It will give you the id in script. 它将为您提供脚本中的ID。
Change the button tag like this 像这样更改按钮标签
<td><button type="button" class="ids" id="btnpartial" value="showdetail" onclick="getview(@prod.Pid)"></button></td>
and the JS function like this 和这样的JS函数
function getview(id) {
debugger;
var productId = id;
});
Try following code. 尝试以下代码。
View 视图
<td><button type="button" class="ids" id="btnpartial" value="showdetail" onclick="getview(@prod.Pid)"></button></td>
Script 脚本
function getview(x)
{
var id = x; //Id will get here.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.