简体   繁体   English

jQuery-附加到表的链接不起作用

[英]jquery - link appended to table doesn't work

Hey guys I have some jquery code that adds a row to a table with a link to remove it and return it back to the select. 大家好,我有一些jquery代码,它们向表中添加了一行,并带有链接以将其删除并将其返回给select。 Here's the code: 这是代码:

$('#addUser').on('click', function (e) {
    var selectedUser = $('#Utilizadores option:selected').text();
    $('#tabela > tbody:last').append('<tr id="'+selectedUser+'"><td>' + selectedUser + '</td><td><a id="removeUser" href="#" class="btn btn-default btn-xs"><i class="glyphicon glyphicon-remove"></i></a></td></tr>');
    $("#Utilizadores option[value='" + selectedUser + "']").remove();
    e.preventDefault();
});

It works! 有用! But now I want to do the reverse action of this. 但是现在我想做相反的事情。 So I was just tying it out and not even the alert goes off. 所以我只是把它绑起来,甚至警报都没有响起。 Here's the remove code: 这是删除代码:

$('#removeUser').on('click', function (e) {
    alert('teste');
    e.preventDefault();
});

The same id I gave to the <a> tag but still nothing gets called. 我给<a>标签赋予了相同的ID,但仍然没有任何调用。 I inspect the element and the id is corret and everything. 我检查了元素,id是corret以及所有内容。

Does anyone know what the problem is? 有人知道问题出在哪里吗?

To anyone who stumbles here and doesn't see the warning above! 对于任何在这里绊倒而看不到以上警告的人!

You can check a Question for the same problem here 您可以在此处检查问题是否存在相同的问题

Try using the live() method 尝试使用live()方法

$('#removeUser').live('click', function (e) {
    alert('teste');
    e.preventDefault();
});

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

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