我无法使用动态生成的按钮删除表中的行.
主要问题是“ alert()”不起作用.
如何捕捉“点击”事件?
jQuery的:
$("#officers-table").on('click', '.remove-officer-button', function(e) {
var whichtr = $(this).closest("tr");
alert('worked'); // Alert does not work
whichtr.remove();
});
HTML / PHP(更新了代码)
<?php if($num_officers > 0): ?>
NameDirectorShareholderSecretaryOptions
=$item->name?> =$item->lastname?>=$item->is_director?>=$item->is_shareholder?>=$item->is_secretary?>EditRemove解决方法:
$(".remove-officer-button").on('click', function(e) {
var whichtr = $(this).closest("tr");
alert('worked'); // Alert does not work
whichtr.remove();
});
编辑
就像大家说的那样,您的代码似乎可以正常工作.检查此JSFiddle:
原始代码:http://jsfiddle.net/mkqU2/1/
您可以使用上面的代码作为替代,但是听起来您还有其他JavaScript错误导致脚本中断.
另外,请确保您的代码在DOM Ready事件中.如果不是,则单击按钮时什么也不会发生.
下面的jsfiddle复制您的错误,如果未包装在DOM Ready事件或窗口加载事件中,则不会触发click事件.
未在DOM中准备就绪:http://jsfiddle.net/mkqU2/2/
标签:button,onclick,html,php,jquery
来源: https://codeday.me/bug/20191031/1975025.html