javascript - 动态创建的 <td> 上的单击事件

标签 javascript jquery html-table

我有下表,它是在开始时使用 onload 函数动态创建的。

<div id="first">
<table class="joma" id="joma">
<tbody>
<tr>
   <th>Id</th>
   <th>V No</th>
   <th>Biboron</th>
   <th>Taka</th>
</tr>
<tr><td class="rid">1</td><td>sss</td><td>222</td><td class="cv">4</td></tr>
<tr><td class="rid">2</td><td>xxx</td><td>2333</td><td class="cv">4</td></tr>
</tbody>
</table>
</div>

现在我需要能够单击 <td class="rid">这应该给出相应的值。在本例中为 1 或 2。

我该怎么做?

我使用了以下方法:

$(document).ready(function() {
    $('.rid').click(function() {
        alert(this);
    });
});



$('.rid').click(function() {
    alert(this);
});

我没有得到任何东西。

最佳答案

使用.on()

$(document).on('click','.rid',function() {
     alert("this");
});

当页面加载时创建 DOM .rid 不是页面的一部分。

因此您无法直接访问它们,您必须使用事件委托(delegate)

关于javascript - 动态创建的 <td> 上的单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18897807/

相关文章:

javascript - 如何将日期字符串转换为带时区的日期?

jQuery:如何在 jQuery 对话框中设置选择?

javascript - 使用 ajax 和 GET 在 DataTable 中使用 MySQL 结果

javascript - Angular : ng-disable not working

javascript - 如何使用 Visual Studio Code 创建要在本地主机上运行的基本内容?

javascript - Firefox WebExtension 导入服务

javascript - Sweetalert2 Ajax - 发布输入数据

javascript - 无法通过单击 id 来提醒 id 按钮

PHP 函数比较两个表中的数据以合并到现有脚本中

php - 如何在基于另一查询结果显示一个查询结果的表中进行复选框?