简体   繁体   English

点击事件不适用于复选框标签

[英]Click event not working with the checkbox tag

i have this code below: 我在下面的代码:

        html.push('<tr class="reservas">');
        html.push('<td> <input type="checkbox" class="checkbox" data-name="' + bla.name + '" data-email="' + bla.email /> </td>'); // atribui os dados a atributos para serem acessados posteriormente por jquery.
        html.push('<td>' + bla.name + '</td>');
        html.push('<td>' + bla.email + '</td>');
        html.push('</tr>');
        $('tbody').append(html.join(""));


$(function(){
    $('.save').on('click', function(){
   });
});

The click event does not work, if i change for the body tag will work, but i need with the checkbox class. click事件不起作用,如果我更改body标记将起作用,但是我需要使用复选框类。

Every help is very appreciated. 非常感谢您的帮助。

Use EventDelegation: 使用EventDelegation:

$(function(){
    $(document).on('click', '.checkbox', function(){
        alert('salvo');
   });
});

From Docs: 从文档中:

Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future. 通过事件委托,我们可以将单个事件侦听器附加到父元素,该元素将为与选择器匹配的所有后代触发,无论这些后代现在存在还是将来添加。

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

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