jQuery On Click无效

puf*_*fin 11 jquery

我无法启动我的事件处理程序.我正在将JSON调用到页面中,如下所示.然后,我希望能够单击我创建的一个按钮,该按钮将执行"hello there"警报.它适用于页面上的每个其他元素,但不适用于<button>.

有人可以帮忙吗?

test.js

$(document).ready(function() {
  $.getJSON('/api/v1/recipe/?format=json', function(data) {
    $.each(data.objects, function(i, recipe) {
      $('table#recipes').append('<tr><td>' + recipe.title + '</td><td>' + recipe.ingredients + '</td><td>' + recipe.method + '</td><td><button class="btn" type="button" id="rmv">remove</button></td></tr>');
      });
    });

    $('rmv').on('click', function() {
      alert('hello there!');
    });
  });
Run Code Online (Sandbox Code Playgroud)

recipe.html

{% extends 'base.html' %}

{% block content %}
<div class="container-fluid">
  <div class="row-fluid">
    <div class="span6">
      <form action='' method="post">{% csrf_token %}
        {{ form.as_p }}
        <input type="submit" value="go baby!">
      </form>
    </div>
    <div class="span6">
      <table class="table table-striped table-bordered" id="recipes">
        <tr>
          <th>Title</th>
          <th>Ingredients</th>
          <th>Method</th>
          <th>Remove</th>
        </tr>
      </table>
    </div>
  </div>
</div>
{% block recipes %}{% endblock recipes %}
{% endblock content %}
Run Code Online (Sandbox Code Playgroud)

JIA*_*JIA 51

你有选择错误它应该是

$('#rmv')
Run Code Online (Sandbox Code Playgroud)

如果你要动态追加元素,你应该像使用它一样

$(document).on('click','#rmv',function(e) {
  //handler code here
});
Run Code Online (Sandbox Code Playgroud)

在ajax成功回调中你的循环可能会生成带有重复id的元素,这是错误的,所以@Alnitak提到你可以像修改cde后那样切换到类选择器

$.getJSON('/api/v1/recipe/?format=json', function(data) {
  $.each(data.objects, function(i, recipe) {
    $('table#recipes').append('<tr><td>' + recipe.title + '</td><td>' + recipe.ingredients + '</td><td>' + recipe.method + '</td><td><button class="btn rmv" type="button" >remove</button></td></tr>');
  });
});
Run Code Online (Sandbox Code Playgroud)

选择器看起来像

$(document).on('click','.rmv',function(e) {
  //handler code here
});
Run Code Online (Sandbox Code Playgroud)

  • 仅供参考,将事件处理程序直接附加到元素本身通常比将所有内容附加到文档对象更有效。 (2认同)

Ano*_*oop 6

试试这个.将此代码移到里面callback

$(document).ready(function() {
        $.getJSON('/api/v1/recipe/?format=json', function(data) {
            $.each(data.objects, function(i, recipe) {
                $('table#recipes').append('<tr><td>' + recipe.title + '</td><td>' + recipe.ingredients + '</td><td>' + recipe.method + '</td><td><button class="btn" type="button" id="rmv">remove</button></td></tr>');
            });

        $('#rmv').on('click', function() {
            alert('hello there!');
        });
        });


    });
Run Code Online (Sandbox Code Playgroud)

或者使用jQuery.on

$(document).on("click", "#rmv",function() {
                alert('hello there!');
            });
Run Code Online (Sandbox Code Playgroud)


Aln*_*tak 5

首先,确保 ID 的元素不超过一个rmv。ID 必须是唯一的,但您似乎要添加多个食谱,每个食谱都有自己的“删除”按钮。

您的事件处理程序注册也有问题 - 在注册事件处理程序之前,您没有等待 AJAX 调用完成(然后添加表行)。

这可以通过在 AJAXsuccess回调中注册事件处理程序来解决,但考虑到您的 ID 的第一个问题,更好的解决方案是使用类 ( .rmv) 而不是 ID,然后使用:

$('#recipes').on('click', '.rmv', function() {
   var $tr = $(this).closest('tr');
   // do something with the current row
});
Run Code Online (Sandbox Code Playgroud)

上面的代码可以在success回调之外注册,因为它使用了事件委托。它依赖于click事件冒泡到封闭表(已经存在),而不是在创建每个按钮时直接在每个按钮上注册事件。