[英]html table, getting text value of first column relative to the row I clicked
So I have a table and i am trying to get the value of the first column based on the row I clicked. 所以我有一张桌子,我试图根据我单击的行获取第一列的值。 I tried this
我试过了
$("#divid").on("click", ".tableid", function(){
alert($(this).find('td:first').text());
});
But it always gives me the first row and first column of my table. 但这总是给我表格的第一行和第一列。 How can I fix this?
我怎样才能解决这个问题?
Thanks 谢谢
The issue is you are selecting the table ".tableid" instead of the clicked row, so the further reference $(this) will be the whole table instead of the clicked row. 问题是您正在选择表“ .tableid”而不是单击的行,因此进一步的引用$(this)将是整个表而不是单击的行。 You have to replace ".tableid" with ".tableid tr".
您必须将“ .tableid”替换为“ .tableid tr”。
Here's the correct example https://jsfiddle.net/nf76rxyq/2/ 这是正确的示例https://jsfiddle.net/nf76rxyq/2/
Html HTML
<div id="divid">
<table class="tableid">
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
</table>
</div>
Javascript Java脚本
$(document).ready(function() {
$("#divid").on("click", ".tableid tr", function() {
alert($(this).find('td:first').text());
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.