javascript - 如何使用 jQuery 添加按钮?
问题描述
我使用 ajax 在我的页面上呈现表格。在表格的最后一行中,我试图显示一个正在处理我的“编辑”功能的按钮,如下所示:
$(".editPersonnel").on("click", function () {
$("#editPersonnelModal").modal("show");
$tr = $(this).closest("tr");
var data = $tr
.children("td")
.map(function () {
return $(this).text();
})
.get();
$("#idPer").val(data[0]);
$("#updateLastName").val(data[1]);
$("#updateFirstName").val(data[2]);
$("#updateJobTitle").val(data[3]);
$("#updateEmail").val(data[4]);
$("#updateDepartment").val(data[5]);
$("#updateLocation").val(data[6]);
console.log(data);
});
我尝试使用 jQuery 呈现该按钮,但是当我这样做时:
$("<td>").html(
'<td><button class="btn btn-info editPersonnel">Edit</button>'
)
该按钮显示在页面上,但不起作用。谁能帮我解决这个问题?我还是个新手……谢谢!
解决方案
您可以使用事件委托,因为元素是动态创建的。
// Replace document with nearest static parent/ancestor, if possible
$(document).on("click", ".editPersonnel", function (){
// ...
});
推荐阅读
- javascript - 在 Chrome 和 OpenLayers 2 中防止“无法在被动事件侦听器中阻止默认值”错误
- android - 挂起功能的执行时间差异巨大
- ggplot2 - Linux中的ggplot2 choropeth ZIP / ZCTA
- javascript - 在地图函数中反应地图函数
- intellij-idea - 如何配置 IntelliJ 以使用 Apache Royale
- java - 如何使用 Spring Data JPA 更新实体中的集合字段?
- javascript - 显示 JavaScript 对话框时 Unity WebGL 崩溃
- node.js - Sequelize :仅返回嵌套包含的多个结果之一
- php - 为什么“了解更多”链接未链接到该页面?
- javascript - 如何向网站提供虚假 GPU 信息