jQuery 在jQuery中获取表的当前rowIndex

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13152369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:19:59  来源:igfitidea点击:

Get current rowIndex of table in jQuery

jqueryjquery-ui

提问by KeVee

My table cell gets highlighted when clicked. I need to find the rowIndex of highlighted cell. I tried doing like this

单击时,我的表格单元格会突出显示。我需要找到突出显示的单元格的rowIndex。我试着这样做

$(".ui-state-highlight").index(); // Results to 0

I tried this too...

我也试过这个...

$('td').click(function(){

    var row_index = $(this).parent().index('tr');

    var col_index = $(this).index('tr:eq('+row_index+') td');

    alert('Row # '+(row_index)+' Column # '+(col_index));

}); 
// Results : Row # -1 Column # -1

I went to through thispost and tried the first answer, still couldn't get the result.

我浏览了这篇文章并尝试了第一个答案,仍然无法得到结果。

回答by Adil

Try this,

尝试这个,

$('td').click(function(){
   var row_index = $(this).parent().index();
   var col_index = $(this).index();
});

If you need the index of table contain td then you can change it to

如果您需要表的索引包含 td 那么您可以将其更改为

var row_index = $(this).parent('table').index(); 

回答by Fil

Since "$(this).parent().index();" and "$(this).parent('table').index();" don't work for me, I use this code instead:

由于“$(this).parent().index();” 和“$(this).parent('table').index();” 不适合我,我使用此代码:

$('td').click(function(){
   var row_index = $(this).closest("tr").index();
   var col_index = $(this).index();
});