简体   繁体   English

jQuery事件未在动态创建的元素上触发

[英]jQuery event not triggering on dynamically created element

Here goes my question.. 这是我的问题。 在此处输入图片说明

/* I am using ajax to dynamically create table */ / *我正在使用ajax动态创建表* /

 $(".n").click(function(){
      var id= $(this).closest('tr').find('td.ide2').html();

         //for displaying the table
         $.ajax({
           type: 'POST',
           url: '<?php echo base_url(); ?>Admin/show', //We are going to make the request to the method "list_dropdown" in the match controller
           dataType:'json',
           data: {'id':id}, //POST parameter to be sent with the tournament id
           success: function(resp) { 

             for(var i=0;i<(resp.length);i++)
              {
                var row = $('<tr></tr>').appendTo($("#unique-list"));

                $('<td />',{text:resp[i]}).appendTo(row);
                $('<td class="off-del glyphicon glyphicon-minus"></td>').appendTo(row);  

             }//end for loop
            } //end success
            });  //end ajax




          $(".off-del").click(function(){
          alert('hello');
          var id= $(this).closest('tr').find($(":first-child")).html();
          console.log(id);
          });
        });

the event click on $(".off-del") is not triggering automatically I have to write in console the name of the event then this event starts functioning. $(".off-del")上的事件单击不会自动触发,我必须在控制台中输入事件的名称,然后此事件才能开始运行。 Is there any issue with class name generating dynamically and how to overcome 动态生成类名是否有问题以及如何克服

After i wrote the name of the event in console it works 在控制台中写下事件的名称后,它可以工作 在此处输入图片说明

Ajax calls are asynchronous. Ajax调用异步的。

Before the elements are appended via ajax, the click handler gets registered, which find no elements with $(".off-del") . 在通过ajax追加元素之前,将注册单击处理程序,该单击处理程序不会使用$(".off-del")找到任何元素。

You should probably use event delegation . 您可能应该使用事件委托

$(document).on('click','.off-del',function(){
    alert('hello');
    var id= $(this).closest('tr').find($(":first-child")).html();
    console.log(id);
});

Instead of $(document) you can use a static parent. 可以使用静态父代代替$(document)

When you set up an event handler in jQuery like this: 当您像这样在jQuery中设置事件处理程序时:

      $(".off-del").click(function(){
        alert('hello');
        var id= $(this).closest('tr').find($(":first-child")).html();
        console.log(id);
      });

you're telling it to go find the elements with class "off-del" in the document right at that moment . 你告诉它去查找文档与类“非德尔”的元素就在那一刻 Your ajax operations will add such elements, but it'll only happen after your attempt to set up the handlers. 您的ajax操作将添加此类元素,但仅在尝试设置处理程序之后才会发生。

Instead of that, you can set up the handler on the document object and take advantage of event bubbling: 相反,您可以在文档对象上设置处理程序并利用事件冒泡:

$(document).on("click", ".off-del", function() {
      alert('hello');
      var id= $(this).closest('tr').find($(":first-child")).html();
      console.log(id);
});

You can set up the event handler that way any time you want, and it'll handle clicks from any ".off-del" element added at any time. 您可以根据需要随时以这种方式设置事件处理程序,它可以处理随时添加的任何“ .off-del”元素的点击。

You are facing this issue because after your dynamically created html you are also supposed to load that event which will bind it to newly created html. 您正面临这个问题,因为在动态创建的html之后,您还应该加载该事件,将其绑定到新创建的html。

So create a function for event and make a call to that function after dynamically created html. 因此,为事件创建一个函数,并在动态创建html之后调用该函数。

While you can use event delegation for this - you could also just register the handler after you've appended all the HTML (inside the success callback) 虽然你可以使用事件代表团这一点-你也可以只登记处理你附加的所有HTML(内侧 success回调)

for(var i=0;i<(resp.length);i++) {
    var row = $('<tr></tr>').appendTo($("#unique-list"));
    $('<td />',{text:resp[i]}).appendTo(row);
    $('<td class="off-del glyphicon glyphicon-minus"></td>').appendTo(row);  
}//end for loop

$(".off-del").click(function(){
      alert('hello');
      var id= $(this).closest('tr').find($(":first-child")).html();
      console.log(id);
});

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

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