表单提交后在表中隐藏按钮


Hide button inside a table after form submit?

因此,我试图在按下delete后创建一行包含删除按钮的行。隐藏。问题是..删除按钮提交了一个表单,我无法预先定义我的按钮类/ID,因为它被多次回显(php脚本读取一个目录,然后将所有文件放在一个表中)

这是当前的脚本,它确实在后台发布了表单,但它没有隐藏元素,我自己尝试了一些东西,但脚本最终隐藏了页面上的所有按钮,否则根本不起作用。。

按钮发出回声:

echo "<input type='submit' value='delete' name='submit'>";

背后的脚本:

$("#delform").submit(function() {
    var url = "../../setdel.php"; // the script where you handle the form input.
    $.ajax({
           type: "POST",
           url: url,
           data: $("#delform").serialize(), // serializes the form's elements.
           success: function(data)
           {
             // location.reload(); // show response from the php script.
           }
         });
    return false; // avoid to execute the actual submit of the form.
});

捕获按钮的onclick,而不是捕获提交。示例:

echo "<input type='submit' value='delete' name='delete' class='trigger_delete'>";
                                            //  ^^ don't name your buttons as submit
                                            // it will conflict to .submit() method

JS上的:

$('.trigger_delete').on('click', function(e){
    e.preventDefault(); // prevent triggering submit
    var url = "../../setdel.php";
    $.ajax({
        type: 'POST',
        url: url,
        data: $("#delform").serialize(),
        success: function(response) {
            console.log(response);
            $(e.target).closest('tr').hide(); // or .fadeOut();
        }(e) // <--- this one
     });
});