jQuery 获取表格中选定行的 id -HTML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5142422/
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 18:37:33  来源:igfitidea点击:

Get id of selected row in a table -HTML

jqueryhtml

提问by Null Pointer

I have a table in my MVC application.I want to get the id of the selected row.I captured the click event of tr using jQuery. The table sample is shown below

我的 MVC 应用程序中有一个表。我想获取所选行的 id。我使用 jQuery 捕获了 tr 的点击事件。表示例如下所示

<table id="resultTable">
 <tr id="first">
  <td>c1</td>      
  <td>c2</td>      
 </tr>
 <tr id="second">
  <td>c3</td>      
  <td>c4</td>      
  </tr>    
</table>

I am using following script to access row click event

我正在使用以下脚本访问行单击事件

 $(document).ready(function () {      
     $('#resultTable tr').click(function (event) {
          alert(this.); //trying to alert id of the clicked row          

     });
 });

But its not working.How can get selected row id .??Any ideas??

但它不起作用。如何获得选定的行 id .??有什么想法吗??

回答by stecb

$(document).ready(function () {      
     $('#resultTable tr').click(function (event) {
          alert($(this).attr('id')); //trying to alert id of the clicked row          

     });
 });

回答by user113716

You were almost there. Just access it directly:

你快到了。直接访问即可:

alert(this.id);