单击注册具有相同类名的多个按钮

onclick registering for multiple buttons with the same class name

本文关键字:按钮 同类 注册 单击      更新时间:2023-09-26

现在,当我单击使用类"btn-danger"的单独按钮时,它将被删除。如何修改我的函数以仅在与正在运行的函数相关时删除任何内容?

function UpdateTrash(wo)
{
    jQuery.ajax({
        type: "POST",
        url: "functions/markTrash.php",
        data: 'wo='+wo,
        cache: false,
        success: function(response)
        {
            //alert("Record successfully updated");
        }
    });
}
$("#dataTables-example").on('click', '.btn-danger', function () {
    $(this).parent().parent().remove();
});
$("#dataTables-example2").on('click', '.btn-danger', function () {
    $(this).parent().parent().remove();
});

按钮:

<td><br><center><button type="button" name="trashButton1" class="btn btn-danger" onClick="UpdateTrash(<?php echo $orow['WorkOrder']; ?>);"/>Trash</button></a></center></td>

其他按钮:

<td><center>
<button type="button" name="rush1" class="btn btn-outline btn-danger" onClick="UpdateRush(<?php echo $orow['WorkOrder']; ?>);"/>Rush</button>
</center><br><center>
<button type="button" name="pool1" class="btn btn-outline btn-info" onClick="UpdatePool(<?php echo $orow['WorkOrder']; ?>);"/>RFP</button></a>
</center></td>

我假设问题出在你的jQuery选择器上。

$(this) -> <button></button>

.parent() -> <div></div>

这是一个工作示例。https://jsfiddle.net/gcLt9d8f/

.HTML:

<div id="dataTables-example">
<button class='btn-danger'>
Button 1
</button>
</div>
<div id="dataTables-example2">
<button class='btn-danger'>
Button 2
</button>
</div>

JS (jQuery(:

$("#dataTables-example").on('click', '.btn-danger', function () {
    $(this).parent().remove();
});
$("#dataTables-example2").on('click', '.btn-danger', function () {
    $(this).parent().remove();
});