简体   繁体   English

js按钮单击未检测到

[英]js button click not detected

I have created a div that when clicked, a messi box pop up, with button "Yes" and "NO".: 我创建了一个div,当单击该div时,会弹出一个消息框,其中包含“是”和“否”按钮。

 var Abutton = document.createElement("div");
                Abutton.className = "Abutton";
                Abutton.onclick = function () {
                    new Messi(
                        'Are you sure you want to close your job?',
                        {
                            title: 'Are you sure?',
                            buttons: [
                                {id: 0, label: 'Yes', val:'Y' , class: 'deleteJob'},
                                {id: 1, label: 'No', val: 'N', class: 'cancelAction'}

                            ]
                        }
                    );
                };
                Abutton.title = "Closed?";
                Abutton.appendChild(document.createTextNode("Close Job"));
                li.appendChild(Abutton);

The "yes " button is tied to this function below . 下面的“是”按钮与此功能相关。 But when I click, nothing happens. 但是当我单击时,什么也没有发生。 The log statement is also not shown. 日志语句也未显示。

  $(".deleteJob").click(function(){
           deleteJob();
           console.log("heelo");
    });

Why is it not working? 为什么不起作用?

Not sure based on just this code but the deleteJob button is being added dynamically and probably doesn't exist at the time you are adding the click event. 不确定仅基于此代码,但deleteJob按钮是动态添加的,在添加click事件时可能不存在。 You should probably use event delegation on the parent element instead. 您可能应该在父元素上使用事件委托。 Based on your code this is the li element. 根据您的代码,这就是li元素。 If it is part of a <ul> maybe put the click event on it like so: 如果它是<ul>一部分,则可以像下面这样放置click事件:

$(function(){
  $('ul').on('click','.deleteJob', function(){
    deleteJob();
    console.log('hello');
  });
});

Again, not 100% sure without seeing the rest of the code. 同样,不能100%确定没有看到其余代码。 But either way, Event Delegation is the way to go. 但是无论哪种方式,事件委托都是要走的路。

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

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