Javascript 删除前确认消息
我想在删除文件之前确认一下。我的代码没有确认,它是这样工作的Javascript 删除前确认消息,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我想在删除文件之前确认一下。我的代码没有确认,它是这样工作的 <form action="delete.php" method="get"> <table> <?php foreach ($files as $file) { ?> <tr> <td><?php echo $fajl['file_name'];?></td> <t
<form action="delete.php" method="get">
<table>
<?php
foreach ($files as $file) {
?>
<tr>
<td><?php echo $fajl['file_name'];?></td>
<td><a href="delete.php?id=<?php echo $file['file_id'];?>"><img src="img/delete.png"/></a></td>
</tr>
<?php } ?>
</table>
</form>
当我点击“删除”时,图像文件被删除。它的工作原理很好,但我想有一个确认之前删除该文件。我试着用Zebra对话框我添加class=“删除”
有没有可能
callback: function() { alert('"Yes" was clicked')}
我打电话
delete.php?id=<?php echo $file['file_id']
delete.php?id=您可以使用函数confirm('Message here')
返回true
或false
$(document).ready(function () {
$(".delete").bind("click", function (e) {
e.preventDefault();
if (window.confirm('Are you sure?'))
{
// .. ajax call ..
}
});
});
编辑:
ajax调用中出现语法错误
$.ajax({
url: 'delete.php',
data: { id: $this.data('id') },
success: function(data){
alert("File deleted");
}
});
此外,没有$this.data('id')
,因此在HTML中:
<a data-id="<?= $file['file_id'] ?>" class="delete"><img src="img/delete.png"/></a>
您不应该为了响应GET请求而删除内容。使用表单。确认和提醒都是非常难看的对话框。大多数人倾向于避免使用它们,因为它们的外观很可怕。@Marcel:没错,但它们功能强大,工作正常,即使它们看起来不漂亮。它们在Firefox中看起来没有那么糟糕(也不会阻止所有窗口/选项卡)。。。(也就是说,它们看起来并不比其他模态对话框差多少)@DavidThomas是的,但大多数客户端/网站所有者都关心这类事情:)我以前在开发过程中使用它们,但现在console.log
和警报/确认的异步替代方法才是解决问题的方法。@MvanGeest问题正是这样。它们在不同浏览器中的外观和工作方式不同。
delete.php?id=<?php echo $file['file_id']
$(document).ready(function () {
$(".delete").bind("click", function (e) {
e.preventDefault();
if (window.confirm('Are you sure?'))
{
// .. ajax call ..
}
});
});
$.ajax({
url: 'delete.php',
data: { id: $this.data('id') },
success: function(data){
alert("File deleted");
}
});
<a data-id="<?= $file['file_id'] ?>" class="delete"><img src="img/delete.png"/></a>