Javascript 表:在行中单击,然后显示列id
我有一张非常简单的桌子。当我在行中单击时,我将成为单击列的id。有什么想法吗Javascript 表:在行中单击,然后显示列id,javascript,jquery,Javascript,Jquery,我有一张非常简单的桌子。当我在行中单击时,我将成为单击列的id。有什么想法吗 <table id="myTable" class="display table-striped"> <thead> <tr> <th id="address">Address</th> <th id="country">Country</th>
<table id="myTable" class="display table-striped">
<thead>
<tr>
<th id="address">Address</th>
<th id="country">Country</th>
<th id="number">number</th>
</tr>
<thead>
<tbody>
<tr>
<td>address1</td>
<td>country1</td>
<td>number1</td>
</tr>
<tr>
<td>address2</td>
<td>country2</td>
<td>number2</td>
</tr>
<tr>
<td>address3</td>
<td>country3</td>
<td>number3</td>
</tr>
</tbody>
</table>
致以最诚挚的问候使用
$(this.index()+1
)获取行索引:
$('#myTable tbody').on('click', 'tr', function () {
alert('You clicked on row '+ ($(this).index() + 1 ) );
});
使用
$(this.index()+1
)获取行索引:
$('#myTable tbody').on('click', 'tr', function () {
alert('You clicked on row '+ ($(this).index() + 1 ) );
});
您可以检查
td
与相关行的索引,然后使用jquery函数查找相关列th
id
$('tr td')。单击(函数(){
var a=$(this.index();
var id=$('tr th').eq(a).attr('id'))
console.log(id);
})
表格,tr,td{
边框:1px纯红;
}
地址
国家
数
地址1
国家1
第一
地址2
国家2
2号
地址3
国家3
3号
您可以检查td
与相关行的索引,然后使用jquery函数查找相关列th
id
$('tr td')。单击(函数(){
var a=$(this.index();
var id=$('tr th').eq(a).attr('id'))
console.log(id);
})
表格,tr,td{
边框:1px纯红;
}
地址
国家
数
地址1
国家1
第一
地址2
国家2
2号
地址3
国家3
3号
使用索引
。索引以0开头,因此添加1
$('myTable tbody')。在('click','tr',function()上{
var row=$(this).index()+1;//索引以0开头,因此添加1
警报(世界其他地区);
});代码>
地址
国家
数
地址1
国家1
第一
地址2
国家2
2号
地址3
国家3
3号
使用索引
。索引以0开头,因此添加1
$('myTable tbody')。在('click','tr',function()上{
var row=$(this).index()+1;//索引以0开头,因此添加1
警报(世界其他地区);
});代码>
地址
国家
数
地址1
国家1
第一
地址2
国家2
2号
地址3
国家3
3号
var$table=$(“#myTable”),
$th=$table.find('thead tr th');
$table.find('tbody tr')。on('click','td',function(){
log($th[$(this.index()].id);
});代码>
地址
国家
数
地址1
国家1
第一
地址2
国家2
2号
地址3
国家3
3号
var$table=$(“#myTable”),
$th=$table.find('thead tr th');
$table.find('tbody tr')。on('click','td',function(){
log($th[$(this.index()].id);
});代码>
地址
国家
数
地址1
国家1
第一
地址2
国家2
2号
地址3
国家3
3号
希望这有帮助
Html:
地址
国家
数
地址1
国家1
第一
地址2
国家2
2号
地址3
国家3
3号
脚本:
<script>
$('#myTable tbody').on('click','td', function () {
var row = $(this).index() ;
alert(row);
});
</script>
$('#myTable tbody')。在('click','td',函数(){
var行=$(this.index();
警报(世界其他地区);
});
在脚本函数中指定“td”。执行此操作将获得特定行中所选列的列id。希望这有帮助
Html:
地址
国家
数
地址1
国家1
第一
地址2
国家2
2号
地址3
国家3
3号
脚本:
<script>
$('#myTable tbody').on('click','td', function () {
var row = $(this).index() ;
alert(row);
});
</script>
$('#myTable tbody')。在('click','td',函数(){
var行=$(this.index();
警报(世界其他地区);
});
在脚本函数中指定“td”。执行此操作,您将获得特定行中所选列的列id。可能重复的可能重复的可能重复的唯一正确答案在我看来,不知道为什么其他人都回这一行index@Pete还请检查我的answer@Pete。它不是行索引。它是与行
相关的列索引。在th
@prasad中查找相关的列id很有用。我知道您正在进行列索引,我只是想知道为什么其他人都在做一个行索引-他们中的一半现在已经改变了它-在我看来,这里唯一正确的答案是,不知道为什么其他人都回这一行index@Pete还请检查我的answer@Pete。它不是行索引。它是与行
相关的列索引。在th
@prasad中查找相关的列id很有用。我知道您正在进行列索引,我只是想知道为什么其他人都在做行索引——他们中的一半现在已经改变了