因此,我试图在按删除后将包含删除按钮的行设为 .hide。 问题是..删除按钮提交了一个表单,我无法预先定义我的按钮类/ID,因为它多次被 echo(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
});
});
关于javascript - 表单提交后隐藏表格内的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25924944/