[英]JavaScript event doesn't fire when nested inside of a dynamically added <div>
Using Play 2.3.x, I am trying to implement a form based on one of Play!'s 2.2.x sample forms that dynamically adds/removes fields using an add/remove button, as described in this answer . 使用Play 2.3.x,我试图实现一个基于Play!的2.2.x示例表单之一的表单,使用添加/删除按钮动态添加/删除字段,如本答案中所述 。
I've successfully implemented a delete button that will delete it's parent div
, using a className, and an add button that adds a new div
to the form based on a hidden template, using an ID. 我已经成功实现了一个删除按钮,它将使用className删除它的父div
,以及一个添加按钮,该按钮使用ID基于隐藏模板向表单添加新div
。 However, in each div
element added to the page, the JavaScript event for the nested delete button doesn't seem to fire or get recognized by the page. 但是,在添加到页面的每个div
元素中,嵌套删除按钮的JavaScript事件似乎不会触发或被页面识别。 I've reviewed a few other questions ( wrap with anonymous function , re-add control at page load , etc) and discussed with other developers, but still can't resolve the issue. 我已经回顾了一些其他问题( 包含匿名函数 , 在页面加载时重新添加控件等)并与其他开发人员讨论过,但仍然无法解决问题。 I've been working with JS for about 2 weeks now, and it's likely that I don't know the right documentation to review to find a resolution. 我已经和JS一起工作了大约两个星期了,我可能不知道要查看的正确文档以找到解决方案。 Any advice regarding resolution, an explanation as to the cause of the issue, and even tips for better coding practice would be appreciated. 任何有关解决方案的建议,关于问题原因的解释,甚至是更好的编码实践的提示都将受到赞赏。
I've stripped out the Play! 我已经剥离了Play! references in an effort to minimize down to the problem and provided an executable version of the same: 引用以尽量减少问题并提供相同的可执行版本:
<div class="well weekly">
<div class="alignRight">
<a class="btn btn-default remove"><i class="glyphicon glyphicon-trash"></i></a>
</div>
<table class="formtable"><tbody>
<tr>
<td colspan="2">title</td>
</tr>
<tr>
<td>category</td>
<td>content</td>
</tr>
</table>
</div>
<div id="template" class="weeklyTemplate">
<div class="alignRight">
<a class="btn btn-default remove"><i class="glyphicon glyphicon-trash"></i></a>
</div>
<table class="formtable"><tbody>
<tr>
<td colspan="2">title</td>
</tr>
<tr>
<td>category</td>
<td>content</td>
</tr>
</table>
</div>
<a class="btn btn-primary add">Add</a>
And this is the JavaScript for the add and delete buttons: 这是添加和删除按钮的JavaScript:
<script>
$('.remove').on('click', function() {
$(this).parents('.weekly').remove();
});
$('.add').on('click', function() {
$("#template").before('<div class="well weekly">' + $('#template').html() + '</div>');
});
</script>
Attach them to the parent: 将它们附加到父级:
$('body').on('click', '.remove', function() {
$(this).parents('.weekly').remove();
});
$('body').on('click', '.add', function() {
$("#template").before('<div class="well weekly">' + $('#template').html() + '</div>');
});
This way the click
event bubbles up to the parent and we catch it there, even from dynamically created objects. 通过这种方式, click
事件会冒泡到父级,我们会在那里捕获它,甚至是动态创建的对象。
I'm using $('body')
selector simply because I can't see from your code example which is the correct parent. 我使用$('body')
选择器只是因为我无法从你的代码示例中看到哪个是正确的父。 You might want to use $('#template')
or $('.formtable')
or some other container. 您可能想要使用$('#template')
或$('.formtable')
或其他一些容器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.