繁体   English   中英

获取从MVC表中单击的行的ID

[英]Get Id of the row clicked from the table in MVC

我想在用户单击按钮时单击行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>

我试过了:-

function getview() {
    debugger;
    var productId = $("#ProductGrid .ids").closest("tr").find("td").eq(0).html();
});

但这是行不通的。

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

$('.ids').click(function() {
    var productId = $(this).closest("tr").find("td").eq(0).text();
});

或者,将值添加到data属性中

<button type="button" class="ids" data-id="@prod.Pid"></button>

和使用

$('.ids').click(function() {
    var productId = $(this).data('id');
});

请注意,当前选择器- $("#ProductGrid .ids")返回所有按钮元素,而不是您单击的按钮元素。

只需更新标签即可。

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

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

它将为您提供脚本中的ID。

像这样更改按钮标签

<td><button type="button" class="ids" id="btnpartial" value="showdetail" onclick="getview(@prod.Pid)"></button></td>

和这样的JS函数

function getview(id) {
    debugger;
    var productId = 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.
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM