gpt4 book ai didi

javascript - 使用 Bootstrap 表获取 'a' 元素的 id

转载 作者:行者123 更新时间:2023-12-03 09:03:18 26 4
gpt4 key购买 nike

我想获取一个元素的 id。我正在使用 Bootstrap 表来显示数据库中的数据列表。当用户单击一行时,我想将用户重定向到其他页面。

这是我的表的声明:

while ($data = pg_fetch_array($res))
{
echo' <tbody>
<tr>
<td><a id="'.$data['id_essai'].'"> //This is the id that I want to get !!
'.$data['id_essai'].'
</a></td>
<td>
'.$data['nom_local_essai'].'
</td>
<td>
'.$data['thematique'].'
</td>
</tr>
</tbody>
';
}

我使用这个函数,当用户单击一行时发生:

 $('table').on('click', 'tr' , function (event) {
console.log($('a').attr('id'));
});

而且我不知道如何获取我的 aattr id。所以我尝试了 $('a').attr('id') 但它不起作用。

请帮忙!

最佳答案

您可以使用以下方法获取单击的 tr 内的 anchor 标记 $(this).find('a')

现在 $(this).find('a').attr('id') 将为您获取 id

  $('table').on('click', 'tr' , function (event) {
console.log($(this).find('a').attr('id'););
});

关于javascript - 使用 Bootstrap 表获取 'a' 元素的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32249315/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com