[英]Get contents of the first cell in the clicked column and the first cell of the clicked row in a HTML table
[英]html table, getting text value of first column relative to the row I clicked
所以我有一張桌子,我試圖根據我單擊的行獲取第一列的值。 我試過了
$("#divid").on("click", ".tableid", function(){
alert($(this).find('td:first').text());
});
但這總是給我表格的第一行和第一列。 我怎樣才能解決這個問題?
謝謝
問題是您正在選擇表“ .tableid”而不是單擊的行,因此進一步的引用$(this)將是整個表而不是單擊的行。 您必須將“ .tableid”替換為“ .tableid tr”。
這是正確的示例https://jsfiddle.net/nf76rxyq/2/
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>
Java腳本
$(document).ready(function() {
$("#divid").on("click", ".tableid tr", function() {
alert($(this).find('td:first').text());
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.