简体   繁体   English

jQuery:为什么未在生成的按钮上调用click函数

[英]jQuery: Why the click function is not called on generated button

Here is a jsFiddle to explain my problem. 这是一个jsFiddle来解释我的问题。 If you try to click on the button "Add an ingredient", the button click event is called. 如果您尝试单击按钮“添加成分”,则将调用按钮单击事件。 My problem is that when I click on the "Create a subtitle", it dynamically created "Add an ingredient" button for each sub-categories. 我的问题是,当我单击“创建字幕”时,它为每个子类别动态创建了“添加成分”按钮。 All button have the same id beginning with "create-ingredient" concatenated with a unique ID so it creates a unique ID for the button. 所有按钮都具有相同的ID,以“ create-ingredient”开头,并带有唯一的ID,因此它为按钮创建了唯一的ID。 I then use a "start with" selector in jQuery to select all button that start with "create-ingredient". 然后,我在jQuery中使用“开始于”选择器来选择所有以“ create-ingredient”开始的按钮。 It just doesn't select the ones that are dynamically created, it just work with the one that was originally in the html page. 它只是不选择动态创建的内容,而仅与HTML页面中的内容一起使用。

Here is the click event not firing up on the dynamic ones: 这是点击事件未在动态事件上触发的事件:

$("[name^='create-ingredient']").click(function() {
    alert('ingredient clicked');
    return false;
});​

What do I need to change so the dynamically created button event gets called? 我需要更改什么才能调用动态创建的按钮事件?

Thanks! 谢谢!

click wires up the event statically. click静态连接事件。 You want on , which triggers the event even on added elements. 您需要on ,即使在添加的元素上也会触发事件。

Note that the syntax for on is slightly different than click . 请注意, on的语法与click略有不同。 You typically use: 您通常使用:

$("container").on("click", "selector", handler);

where "container" selects a static container element (can just be "document"), and "selector" is the element you want to target. 其中“容器”选择一个静态容器元素(可以只是“文档”),而“选择器”是您要定位的元素。

Forked Fiddle. 分叉的小提琴。


Edit 编辑

Clarifying the above per David's comment: the use of on actually attaches an event handler to "container", and delegates the callback to any elements inside the container that match "selector". 根据David的评论澄清以上内容: on的使用实际上将事件处理程序附加到“容器”,并将回调委托给容器内与“选择器”匹配的任何元素。 This is a case where it's well worth reading the JQuery docs : 在这种情况下,非常值得阅读JQuery文档

When a selector is provided, the event handler is referred to as delegated. 提供选择器后,事件处理程序称为“委托”。 The handler is not called when the event occurs directly on the bound element, but only for descendants (inner elements) that match the selector. 当事件直接发生在绑定元素上时,不调用处理程序,而仅对与选择器匹配的后代(内部元素)进行调用。 jQuery bubbles the event from the event target up to the element where the handler is attached (ie, innermost to outermost element) and runs the handler for any elements along that path matching the selector. jQuery使事件从事件目标一直冒泡到附加了处理程序的元素(即,最内层元素到最外层元素),并沿该路径运行与选择器匹配的任何元素的处理程序。

Try: jQuery (>1.7 ) 试试:jQuery(> 1.7)

$(document).on('click', '[name^="create-ingredient"]', function() {
    alert('ingredient clicked');
    return false;
});

Or for older jquery (<1.7) 或用于较旧的jQuery(<1.7)

$("[name^='create-ingredient']").live(function() {
    alert('ingredient clicked');
    return false;
});

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

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