17

我无法让我的事件处理程序触发。我将 JSON 调用到页面中,如下所示。然后我希望能够单击我创建的按钮之一,该按钮将执行“你好”警报。它适用于页面上的所有其他元素,但不适用于<button>.

任何人都可以帮忙吗?

测试.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!');
    });
  });

食谱.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 %}
4

6 回答 6

74

你选错了应该是

$('#rmv')

如果您要动态附加元素,则应该像这样使用它

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

您在 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>');
  });
});

选择器看起来像

$(document).on('click','.rmv',function(e) {
  //handler code here
});
于 2013-04-15T18:57:23.173 回答
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!');
        });
        });


    });

或使用 jQuery.on

$(document).on("click", "#rmv",function() {
                alert('hello there!');
            });
于 2013-04-15T18:57:27.930 回答
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
});

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

于 2013-04-15T18:58:59.927 回答
1

如果您使用元素的 id 来触发任何调用,则 Jquery 函数需要附加一个“#”符号。所以改变

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

$('#rmv').on('click', function() {
        alert('hello there!');
    });
于 2013-04-15T19:00:10.703 回答
0
$.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!');
            });
        });

在您的 ajax 请求的成功函数中附加事件处理程序。

编辑 - 根据其他人,您的选择器rmv是错误的,应该是#rmv

于 2013-04-15T18:57:34.273 回答
0
$('#rmv', table).on('click', function(){
     alert('hello there!');
});
于 2013-04-15T18:59:17.283 回答