简体   繁体   English

将click事件绑定到使用jQuery on()动态加载的实体

[英]Binding a click event to entities loaded dynamically using jQuery on()

This is a common question asked many times but I am not able to find a solution for my problem. 这是一个经常被问到的常见问题,但是我无法找到解决问题的方法。

I have a HTML table in which the data is loaded using ajax. 我有一个HTML表,其中使用ajax加载了数据。 I need to bind a click event to the delete link. 我需要将click事件绑定到删除链接。 Since this data is loaded using ajax I have tried using jQuery on() method as shown below. 由于此数据是使用ajax加载的,因此我尝试使用jQuery on()方法,如下所示。

$('.datagrid').on('load', '.action', function(){
        $('.action a#deleteTrigger').each(function() { 
            $(this).click(function() {
                alert('called..');
            })
        });
    })

But this doesn't work. 但这是行不通的。 Below is a simple illustration of my HTML DOM. 以下是我的HTML DOM的简单说明。

<table class='datagrid'>
    <tr id="row-1">
       <td class="">
          1                         
       </td>
       <td class="">
          admin                         
       </td>
       <td>
         <a id="deleteTrigger" href="#">Delete</a>
       </td>
    </tr>
    <tr id="row-2">
       <td class="">
          1                         
       </td>
       <td class="">
          admin                         
       </td>
       <td>
         <a id="deleteTrigger" href="#">Delete</a>
       </td>
    </tr>
    <tr id="row-3">
       <td class="">
          1                         
       </td>
       <td class="">
          admin                         
       </td>
       <td class='action'>
         <a id="deleteTrigger" href="#">Delete</a>
       </td>
    </tr>
</table>

Can anyone please help me with this? 谁能帮我这个忙吗?

There is no load event triggered by loading via ajax so trying to use it won't accomplish anything. 通过ajax加载不会触发任何load事件,因此尝试使用它不会完成任何操作。

In addition, it looks like you have duplicate ids in the content. 此外,您的内容中似乎有重复的ID。 An id must be unique in the document and trying to use a selector to find duplicate ids won't work. ID在文档中必须是唯一的,并且尝试使用选择器查找重复的ID是行不通的。 You should be using a class name. 您应该使用一个类名。

Probably, the better way to solve the event handlers on dynamically loaded elements is to use delegated event handling on a class name in the content. 解决动态加载元素上的事件处理程序的更好方法可能是对内容中的类名称使用委托事件处理。

If you change your HTML to a class name like this: 如果将HTML更改为这样的类名:

<a class="deleteTrigger" href="#">Delete</a>

Then, assuming the top of the table isn't loading dynamically you can bind to it with delegated event handling like this: 然后,假设表的顶部未动态加载,则可以使用委托事件处理将其绑定到该表,如下所示:

$(".datagrid").on("click", ".deleteTrigger", function(e) {
    // click handler for delete button
});

If even the top of the table is loaded dynamically, then you can change to other static parent higher up in the DOM (you should pick a parent as close to the content as possible as long as it is not dynamically loaded) like this: 如果甚至表的顶部是动态加载的,那么您也可以在DOM中更改为更高的其他静态父级(只要不动态加载,就应该选择一个尽可能靠近内容的父级),如下所示:

$("selector of some static parent").on("click", ".deleteTrigger", function(e) {
    // click handler for delete button
});

You can see these other references on delegated event handling with .on() : 您可以使用.on()查看有关委托事件处理的其他参考:

jQuery .live() vs .on() method for adding a click event after loading dynamic html jQuery .live()vs .on()方法,用于在加载动态html后添加click事件

jQuery .on does not work but .live does jQuery .on不起作用,但.live起作用

Does jQuery.on() work for elements that are added after the event handler is created? jQuery.on()是否可用于创建事件处理程序后添加的元素?

use class instead of id 使用类而不是id

<td class='action'>
         <a class="deleteTrigger" href="#">Delete</a>
</td>

and in javascript 和在JavaScript

$(document).on("click",".deleteTrigger",function(e){
          alert('called..');
          e.preventdefault()
});

this may help you. 这可能对您有帮助。

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

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