表单提交后隐藏表内的按钮?

时间:2014-09-19 01:24:27

标签: javascript php jquery html forms

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

这是当前的脚本,它确实在后台发布了表单,但它并没有隐藏元素,我自己尝试了一些东西但是脚本最后隐藏了页面上的所有按钮或者它赢了& #39; t work at all ..

按钮响起了回声&#d;

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.
});

1 个答案:

答案 0 :(得分:0)

抓住按钮的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
     });
});
相关问题