[英]Prevent table row onclick event to fire when clicking button inside the row
I have a table row with ONCLICK event (that toggles additional data below).我有一个带有 ONCLICK 事件的表格行(切换下面的附加数据)。 Inside one of the row cells I have a button (when clicked is performing an AJAX action).在其中一个行单元格中,我有一个按钮(单击时正在执行 AJAX 操作)。 When I click on the button - the Row's onclick event also fires and what happens is that the additional data appears before the AJAX call completes (which is a bad behavior for me).当我单击按钮时 - Row 的 onclick 事件也会触发,并且会在 AJAX 调用完成之前出现附加数据(这对我来说是一种不好的行为)。 Any ideas how can I solve this elegantly?任何想法如何优雅地解决这个问题? (without identifying and coding it into the row's onclick code) Thanks (无需识别并将其编码到行的 onclick 代码中)谢谢
If you dislike the javascript solution, in many cases a CSS solution is possible as well:如果您不喜欢 javascript 解决方案,在许多情况下,CSS 解决方案也是可能的:
style="pointer-events: none;"
That will supress the <tr>
onclick event.这将抑制<tr>
onclick 事件。
However, in some cases I still had issues where it didn't seem to work.但是,在某些情况下,我仍然遇到它似乎不起作用的问题。 In the majority of cases I just use the style above.在大多数情况下,我只使用上面的样式。
If you use jQuery, you can just do this instead:如果你使用 jQuery,你可以这样做:
tbody.on('click', 'tr', function (e) {
let target = $(e.target);
if (target.is('i') || target.is('button') || target.is('a') || target.hasClass('select-checkbox')) {
return;
}
//your stuff here
});
<table>
<thead>
<tr>
<th>Card_ID</th>
<th>Card_Number</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr style="pointer-events: none;">
<td>3</td>
<td>12345</td>
<td>Active</td>
<td><button style="pointer-events: auto;" type="button" id="Inactive" class="btn btn-sm"></button></td>
</tr>
</tbody>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.