使用javascript/jquery从表的给定行提取Cell/Td/tabledata值
下面是我在Asp.net MVC razor视图中使用的html表使用javascript/jquery从表的给定行提取Cell/Td/tabledata值,javascript,jquery,html,asp.net-mvc,html-table,Javascript,Jquery,Html,Asp.net Mvc,Html Table,下面是我在Asp.net MVC razor视图中使用的html表 <table class="table-striped table-bordered"> <thead> <tr> <th class="col-md-2"> Id </th> <th class="col-md-4"> Description
<table class="table-striped table-bordered">
<thead>
<tr>
<th class="col-md-2">
Id
</th>
<th class="col-md-4">
Description
</th>
<th class="col-md-3">
Quantity
</th>
<th class="col-md-3">
AssetType
</th>
</tr>
</thead>
<tbody>
@foreach (var i in Model)
{
<tr>
<td class="col-md-2">
@i.Id
</td>
<td class="col-md-4">
@i.Description
</td>
<td class="col-md-3">
@i.Count
</td>
<td class="col-md-3">
@i.AssetType
</td>
<td>
<a onclick="getId()">Edit</a>
</td>
</tr>
}
</tbody>
</table>
身份证件
描述
量
资产类型
@foreach(模型中的var i)
{
@i、 身份证
@i、 描述
@i、 计数
@i、 资产类型
编辑
}
我的Js代码
<script type="text/javascript">
var getId = function () {
//get current row
var currentRow = $(this).closest('tr');
// get the id from the current row. or is there any better way ?
}
</script>
var getId=函数(){
//获取当前行
var currentRow=$(this).closest('tr');
//从当前行获取id。或者有更好的方法吗?
}
您好,在上面的代码中。我所要做的就是当用户选择表中给定行的编辑链接时。我想提取所选行的id值
谁能给我介绍一下这个吗?我看到过一些文章,其中提到如何从每一行中获取给定的单元格值,但没有找到解释如何从给定行中提取数据单元格值的文章 您已经拥有了它,因为您正在从服务器端生成HTML,当用户单击“将id传递给函数”以执行任何您想要的操作时
<td>
<a onclick="getId('@i.Id')">Edit</a>
</td>
function getId(id) {...}
编辑
函数getId(id){…}
或者,如果您愿意,您可以使用以下内容:
<a onclick="getId(this)">Edit</a>
function getId(dom){
var id = $(dom).parent().find('.col-md-2').html();
}
编辑
函数getId(dom){
var id=$(dom.parent().find('.col-md-2').html();
}
您已经拥有了它,因为您正在从服务器端生成HTML,当用户单击“将id传递给函数”以执行任何您想要的操作时
<td>
<a onclick="getId('@i.Id')">Edit</a>
</td>
function getId(id) {...}
编辑
函数getId(id){…}
或者,如果您愿意,您可以使用以下内容:
<a onclick="getId(this)">Edit</a>
function getId(dom){
var id = $(dom).parent().find('.col-md-2').html();
}
编辑
函数getId(dom){
var id=$(dom.parent().find('.col-md-2').html();
}
您可以在编辑链接中将id值设置为数据id
属性,如下所示
<a data-id="@i.Id" class="edit-button" href="#">Edit</a>
工作小提琴:您可以在编辑链接中将id值设置为
数据id
属性,如下所示
<a data-id="@i.Id" class="edit-button" href="#">Edit</a>
工作小提琴:
var id=currentRow.children('td').eq(0.text()代码>谢谢Stephen Mueckevar id=currentRow.children('td').eq(0.text()代码>谢谢Stephen Muecke