【发布时间】:2013-01-09 08:48:29
【问题描述】:
我无法使用动态生成的按钮删除表格中的一行。 主要问题是“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(更新代码)
<table id="officers-table" class="ui-widget ui-widget-content">
<?php if($num_officers > 0): ?>
<thead>
<tr class="ui-widget-header ">
<th>Name</th>
<th>Director</th>
<th>Shareholder</th>
<th>Secretary</th>
<th colspan="2">Options</th>
</tr>
</thead>
<?php endif; ?>
<tbody>
<?php foreach ($officers as $item): ?>
<tr>
<td><?=$item->name?> <?=$item->lastname?></td>
<td><?=$item->is_director?></td>
<td><?=$item->is_shareholder?></td>
<td><?=$item->is_secretary?></td>
<td>Edit</td>
<td><button type="button" value="<?=$item->id?>" class="remove-officer-button">Remove</button></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
【问题讨论】:
-
您可能遇到了错误。检查你的 JS 控制台。
-
控制台没有显示任何错误
-
click事件是否会触发? -
$("#officers-table")可能为空。你确定你有一个具有该 ID 的元素吗? -
检查你的表是否真的有
id="officers-table"属性
标签: php jquery html button onclick