如何判断在表格中点击哪个行号? [英] How to tell which row number is clicked in a table?

查看:77
本文介绍了如何判断在表格中点击哪个行号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似以下的表:

 < table> 
< tr>
< td> 1< / td>< td> 1< / td>< td> 1< / td>
< / tr>
< tr>
< td> 2< / td>< td> 2< / td>< td> 2< / td>
< / tr>
< tr>
< td> 3< / td>< td> 3< / td>< td> 3< / td>
< / tr>
< / table>

当用户点击表格时,如何获取该行的索引(<$ c

例如,当我点击第一个 tr (在上面的表格中 1 s),它应该选择它并返回 1

解决方案

这会得到点击行的索引,从一开始:



< $($'$'$>'$ the $')。find('tr')。click(function(){
alert('您点击了行'+($(this))。 index()+ 1));
});

如果您想返回存储在每行第一个单元格中的数字:

  $('#thetable')。find('tr')。click(function(){
var row = $(this ).find('td:first')。text();
alert('您点击了'+ row);
});


I have a table like the following:

<table>
   <tr>
      <td>1</td><td>1</td><td>1</td>
   </tr>
   <tr>
      <td>2</td><td>2</td><td>2</td>
   </tr>
   <tr>
      <td>3</td><td>3</td><td>3</td>
   </tr>
</table>

When a user clicks on the table, how can I get the index of this row (tr element)?

For example, when I click on the first tr (with 1s in the above table), it should pick it up and return 1.

解决方案

This would get you the index of the clicked row, starting with one:

$('#thetable').find('tr').click( function(){
  alert('You clicked row '+ ($(this).index()+1) );
});

If you want to return the number stored in that first cell of each row:

$('#thetable').find('tr').click( function(){
  var row = $(this).find('td:first').text();
  alert('You clicked ' + row);
});

这篇关于如何判断在表格中点击哪个行号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆