Jquery 获取按钮所在行中第一个td元素的值
我有一张这样的表格:Jquery 获取按钮所在行中第一个td元素的值,jquery,Jquery,我有一张这样的表格: <table> <thead> <tr> <th>Id</th> <th>Name</th> <th>Company</th> <th>Position</th> <th>Start Dat
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Company</th>
<th>Position</th>
<th>Start Date</th>
<th>Role</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td><?php info displayed with php ?></td>
<td><?php info displayed with php ?></td>
<td><?php info displayed with php ?></td>
<td><?php info displayed with php ?></td>
<td><?php info displayed with php ?></td>
<td><?php info displayed with php ?></td>
<td><a href="#" class="usr-edit">Edit</a></td>
</tr>
</tbody>
</table>
可以在with table元素中使用dom引用来获取所需的单元格
var td = this;
var tr = this.parentNode;
var firstTd = tr.cells[0];
可以在with table元素中使用dom引用来获取所需的单元格
var td = this;
var tr = this.parentNode;
var firstTd = tr.cells[0];
$('.usr edit')。单击(函数(){
var a=$(this.nexist('tr').find('td:nth child(1)).text();
控制台日志(a);
});代码>
身份证件
名称
单位
位置
开始日期
角色
1.
2.
3.
4.
5.
6.
$('.usr edit')。单击(函数(){
var a=$(this.nexist('tr').find('td:nth child(1)).text();
控制台日志(a);
});代码>
身份证件
名称
单位
位置
开始日期
角色
1.
2.
3.
4.
5.
6.
简单
$('.usr-edit').click(function() {
var a = $(this).parent().find('td:first-child').text();
console.log(a);
});
编辑:要找到tr
父项,您可以这样做
$('.usr-edit').click(function() {
var a = $(this).parents('tr').find('td:first-child').text();
console.log(a);
});
而且是一种肮脏的方式
$('.usr-edit').click(function() {
var a = $(this).parent().parent().find('td:first-child').text();
console.log(a);
});
简单
$('.usr-edit').click(function() {
var a = $(this).parent().find('td:first-child').text();
console.log(a);
});
编辑:要找到tr
父项,您可以这样做
$('.usr-edit').click(function() {
var a = $(this).parents('tr').find('td:first-child').text();
console.log(a);
});
而且是一种肮脏的方式
$('.usr-edit').click(function() {
var a = $(this).parent().parent().find('td:first-child').text();
console.log(a);
});
您可以在
上添加类,如
并使用:
$('.yourClass').find('td:first-child').text();
您可以在
上添加类,如
并使用:
$('.yourClass').find('td:first-child').text();
使用$(this).closest('tr').find('td:nth child(1)).text()代码>@guradio成功了!我从来没有想过要去tr,然后是第一行。@j08691它只是提醒一个空框,没有数据。Ug,忽略我之前的评论。该链接没有以前的同级。首先,您需要在DOM中至少提升一个级别。@Mr.Smithyyy我给出了一个答案,很高兴帮助快乐的编码伙伴:)使用$(this.nest('tr')。find('td:nth child(1)')).text()代码>@guradio成功了!我从来没有想过要去tr,然后是第一行。@j08691它只是提醒一个空框,没有数据。Ug,忽略我之前的评论。该链接没有以前的同级。您需要首先在DOM中至少提升一个级别。@Mr.Smithyyy我给出了一个答案很高兴帮助快乐的编码伙伴:)先生,类为usr edit
的元素的父元素是td
这将是一个错误的捕获!更新!:)快乐的编码伙伴:)先生,类为usr edit
的元素的父元素是td
这将是错误的好捕获!更新!:)快乐的编码伙伴:)