Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jQuery添加按钮?_Javascript_Html_Jquery_Ajax_Button - Fatal编程技术网

Javascript 如何使用jQuery添加按钮?

Javascript 如何使用jQuery添加按钮?,javascript,html,jquery,ajax,button,Javascript,Html,Jquery,Ajax,Button,我正在使用ajax在我的页面上呈现一个表。在表格的最后一行,我试图显示一个按钮,该按钮正在处理我的“编辑”功能,如下所示: $(".editPersonnel").on("click", function () { $("#editPersonnelModal").modal("show"); $tr = $(this).closest("tr"); var data = $tr .chil

我正在使用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>'
            )
$(“”).html(
“编辑”
)
按钮显示在页面上,但不起作用。 有人能帮我解决这个问题吗? 我还是个新手。。。
谢谢

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

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

旁注:当试图附加到读取数据但由于某种原因模式仍然不显示的
时,您有一个额外的前导
,或者它显示但在我的当前模式下。我马上回来!是的,这很有效,模态显示在打开的模态下!谢谢你的帮助@池池黄很乐意帮忙。