【问题标题】:How to add a button using jQuery? [duplicate]如何使用 jQuery 添加按钮? [复制]
【发布时间】:2021-04-17 20:20:12
【问题描述】:

我正在使用 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>'
            )

按钮显示在页面上,但不起作用。 谁能帮我解决这个问题? 我还是个新手…… 谢谢!

【问题讨论】:

  • 旁注:尝试附加到 &lt;td&gt; 时,您有一个额外的前导 &lt;td&gt;

标签: javascript html jquery ajax button


【解决方案1】:

您可以使用事件委托,因为元素是动态创建的。

// Replace document with nearest static parent/ancestor, if possible
$(document).on("click", ".editPersonnel", function (){
   // ...
});

【讨论】:

  • 读取了数据,但由于某种原因模态仍然不显示?
  • 或者它可能显示但它在我当前的模式下。我马上就回来!
  • 是的,这行得通,模态显示在打开的模态下!感谢您的帮助!
  • @ChiChiHuang 很高兴为您提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-14
  • 2021-05-24
  • 1970-01-01
相关资源
最近更新 更多