简体   繁体   English

动态表行,将click事件委托给新添加的元素

[英]Dynamic table row, delegation of click event to newly added elements

First of all just want to mention that I have been through almost every similar Q&A, and non of solutions and suggestions helped me in my case. 首先,我只想提一下,我几乎已经完成了所有类似的问答,并且没有解决方案和建议对我有所帮助。

I have this construction DEMO and bellow the code that adds a new element to the table. 我有这个构造DEMO和下面的代码,向表中添加一个新元素。 I like the new element, which is a tr and td, have the same functionality as those already existing. 我喜欢新元素,​​它是tr和td,具有与现有元素相同的功能。 I tried live() and delegate(), nothing works. 我试过live()和delegate(),什么都行不通。 All the suggestions and tutorials pointing towards on() function which I am already using, but still does not work. 所有的建议和教程指向我已经使用的on()函数,但仍然不起作用。 What am I doing wrong? 我究竟做错了什么? Please, any suggestions would be highly appreciated. 请高度赞赏任何建议。 Code below adds new element: 下面的代码添加了新元素:

$(function(){
    $('#add').on('click',function() {
        $('#edit-tb tr:last').after('<tr><td><button class="up">Up</button></td><td><span class="times" data-myval="1"></span>Goe</td><td><button class="down">Down</button></td></tr>');
    });
});

Your event delegation is not correct. 您的活动授权不正确。 Try like following. 尝试以下。

$(document).on('click', '.up', function () {
    var count = $(this).closest('tr').find('.times').attr('data-myval');
    count++;
    if (count > 4) {
        count--;
        $('#max').modal('show');
        $(this).closest('tr').find('.times').text(4 + ' x ');
    }
    $(this).closest('tr').find('.times').attr('data-myval', count);
    $(this).closest('tr').find('.times').text(count + ' x ');
});

$(document).on('click', '.down', function () {
    var count = $(this).closest('tr').find('.times').attr('data-myval');
    count--;
    parseInt($(this).closest('tr').find('.times').attr('data-myval', count))
    $(this).closest('tr').find('.times').text(count + ' x ');
    if (count < 2) {
        $(this).closest('tr').find('.times').text('');
    }
    if (count < 1) {
        $(this).parents('tr').remove();
    }
});

UPDATED FIDDLE 更新后的

Event delegation captures the event on a container, but only invokes the callback if the target element or one of his parents has a certain selector. 事件委托在容器上捕获事件,但仅在目标元素或其父节点之一具有特定选择器时才调用回调。 To delegate an event, you attach the event to the parent using .on() , but add a 2nd parameter, which states the target. 要委派事件,请使用.on()将事件附加到父.on() ,但添加第2个参数,该参数表示目标。 For example: 例如:

  $('.table') // container
      /* event , target, handler */
    .on('click', '.up' , function() {});

You should attach the event handler to a close container, the .table or event .table > tbody if you want the binding to ignore the header. 如果希望绑定忽略标头,则应将事件处理程序附加到关闭容器, .table或事件.table > tbody

Demo ( fiddle ): 演示( 小提琴 ):

$(function() {
  $('.table')
    .on('click', '.up', function() {
        var element = $(this);
      var count = element.closest('tr').find('.times').attr('data-myval');
      count++;
      if (count > 4) {
        count--;
        $('#max').modal('show');
        element.closest('tr').find('.times').text(4 + ' x ');
      }
      element.closest('tr').find('.times').attr('data-myval', count);
      element.closest('tr').find('.times').text(count + ' x ');

    })
    .on('click', '.down', function() {
        var element = $(this);
      var count = element.closest('tr').find('.times').attr('data-myval');
      count--;
      parseInt(element.closest('tr').find('.times').attr('data-myval', count))
      element.closest('tr').find('.times').text(count + ' x ');
      if (count < 2) {
        element.closest('tr').find('.times').text('');
      }
      if (count < 1) {
        element.parents('tr').remove();
      }
    });
});

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

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