简体   繁体   English

嵌套在动态添加的内部时,JavaScript事件不会触发<div>

[英]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')或其他一些容器。

http://api.jquery.com/on/ http://api.jquery.com/on/

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

相关问题 div删除并动态添加后如何触发事件? - How to fire a event when a div got removed and added dynamically? 当 javascript function 添加到 web 页面时,ASP.Net OnTextChanged 事件不会触发 - ASP.Net OnTextChanged event doesn't fire when javascript function added to web page JavaScript/jQuery+日期时间选择器:动态添加的输入不显示触发日期时间选择器。 - JavaScript/jQuery+datetime picker: Dynamically added input doesn't display fire datetime picker. 当有多个动态添加的按钮时,onclick事件不会触发 - onclick event won't fire when there is more than one dynamically added button 使用 jQuery 动态添加 class 在表单上触发 javascript 事件 - Fire javascript event on form with dynamically added class using jQuery 当 DIV 在视图中时触发 JavaScript 事件 - Fire JavaScript Event When a DIV is in View javascript“ mousemove”事件有时不会触发 - javascript “mousemove” event sometimes doesn't fire 按钮onclick事件不会在javascript中触发 - Button onclick event doesn't fire in javascript 子div的onclick事件:动态添加的子div的onclick不能按预期工作 - Child div onclick event: onclick on dynamically added children divs doesn't work as intended 活动不会触发 - Event doesn't fire
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM