简体   繁体   English

第一次单击动态创建的表后,on.click 不起作用

[英]on.click not working after first click on dynamically created table

I dynamically create a table full of links of 'actors', which allows to pull the actors information into a form to delete or update the row.我动态创建了一个充满“演员”链接的表格,它允许将演员信息拉入表单以删除或更新行。 The delete button only pops up when you select an actor.删除按钮仅在您选择演员时弹出。

I'm able to click a row, pull the information into the forms, and delete it on first try.我可以单击一行,将信息拉入表单,并在第一次尝试时将其删除。 However when I attempt to add a new 'Actor' and then delete it, or just delete an existing 2nd row, the button 'Delete Actor' doesn't work.但是,当我尝试添加一个新的“演员”然后将其删除,或者只是删除现有的第二行时,“删除演员”按钮不起作用。 It's only after the first successful delete does the button no longer work.只有在第一次成功删除后,按钮才不再起作用。

var addActor = function() {
    // Creates a table of links for each added actor  with an id based from # of actors
    $("#actorsTable").append("<tr><td><a href='' onclick='deleteActor(this)' class='update' data-idx='" + actors.length + "'>" + newActor.fName + " " + newActor.lName + "</a></td></tr> ");

    $(".update").off("click");
    $(".update").on("click", selectActor);
};


var deleteActor = function(e) {
    $("#deleteActor").on('click', function(event) {
        event.preventDefault();
        var row = e.parentNode.parentNode;
        row.parentNode.removeChild(row);
        clearForm(actorForm);
        actorState("new");
    });
};

I'm new to jQuery/javascript, and I'm pretty sure its due to the change in DOM, but I just don't know what to change to make it work.我是 jQuery/javascript 的新手,我很确定这是由于 DOM 的变化,但我只是不知道要改变什么才能使它工作。

Here is an Example of it in action这是它在行动中的一个例子

Try尝试

var deleteActor = function(e) { var deleteActor = function(e) {

$("#deleteActor").unbind();
$("#deleteActor").on('click', function(event) {
    event.preventDefault();
    var row = e.parentNode.parentNode;
    row.parentNode.removeChild(row);
    clearForm(actorForm);
    actorState("new");
});

}; };

Here is the link for unbind.这是解绑的链接。 http://api.jquery.com/unbind/ http://api.jquery.com/unbind/

The problem is because you're adding another click handler (in jQuery) within the click handler function run from the onclick attribute.问题是因为您正在从onclick属性运行的单击处理程序函数中添加另一个单击处理程序(在 jQuery 中)。 You should use one method or the other, not both.您应该使用一种方法,而不是同时使用两种方法。 To solve the problem in the simplest way, just remove the jQuery code from the deleteActor() function:要以最简单的方式解决问题,只需从deleteActor()函数中删除 jQuery 代码:

var deleteActor = function(e) {
    var row = e.parentNode.parentNode;
    row.parentNode.removeChild(row);
    clearForm(actorForm);
    actorState("new");
};

when you add html dynamically you need to attach the event to the parent static element like so:当您动态添加 html 时,您需要将事件附加到父静态元素,如下所示:

$("#actorsTable").on("click","a.update", function() {
  $(this).closest("tr").remove();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM