使用javascript/jquery从表的给定行提取Cell/Td/tabledata值
Extract a Cell/Td/tabledata value from a given row of a table using javascript/jquery
这是我在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
</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>
My Js Code
<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>
Hi在以上代码中。我所要做的就是当用户选择表中给定行的编辑链接时。我想提取所选行的id值。
谁能在这个问题上给我指教一下?我看过一些介绍如何从每行获取给定单元格值的文章,但没有找到解释如何从给定行提取数据单元格值的文章。您已经拥有了它,因为您正在从服务器端生成HTML,当用户单击时将id传递给函数以执行您想要的任何操作。
<td>
<a onclick="getId('@i.Id')">Edit</a>
</td>
function getId(id) {...}
或者如果你愿意,你可以这样写:
<a onclick="getId(this)">Edit</a>
function getId(dom){
var id = $(dom).parent().find('.col-md-2').html();
}
您可以在Edit链接中将id值设置为data-id
属性,如下所示
<a data-id="@i.Id" class="edit-button" href="#">Edit</a>
将click事件处理程序添加到编辑链接中,您可以使用$(this).data('id')
<script type="text/javascript">
$('.edit-button').on('click', function (e) {
e.preventDefault();
alert($(this).data('id'));
});
</script>
工作小提琴:http://jsfiddle.net/ds4t6jur/
相关文章:
- 在数据提取完成之前进行页面渲染
- Google电子表格getValue([cell containing ])不返回制表符
- 如何在Javascript中从字符串中提取某些单词
- RegEx JavaScript:数字后的符号提取
- 如何在内联依赖项并将图像转换为dataURI的情况下完全提取网页
- Regex提取URL返回数组的一部分;未定义”;
- 如何提取“;href"最近列表项中的属性值
- 使用时刻.js从日期时间中提取时间
- 通过javascript从会话中提取用户名
- HTML页面如何提取通过表单传递的参数
- 是否可以自动拉取“随机”;标签“;从一长串文本中提取
- innerHTML的子字符串可以在初始调用时提取吗
- 如何使用phonegap从安卓设备中提取联系人的字母顺序
- 如何从特定页面中提取php变量值
- 从字符串中提取年份的Javascript正则表达式
- 如何使用JavaScript提取此url的一部分
- 使用BeautifulSoup从Javascript中提取文本以获得关注者数量
- 使用javascript从字符串中提取浮点数
- 如何使用另一个字符串作为模板从字符串中提取数据
- 使用javascript/jquery从表的给定行提取Cell/Td/tabledata值