Javascript警报单击以删除在IE8中不起作用

Javascript alert click to delete not working in IE8

本文关键字:IE8 不起作用 删除 单击 Javascript      更新时间:2023-09-26

我有一个JavaScript,当我点击删除时,它会弹出确认,在我删除之前,它在Mozilla Firefox和Google Chrome中运行良好。但是当我在IE8中单击删除时,它会弹出显示确认,并且当文件被删除时,它拒绝删除。有人有解决方法吗?这是我下面的片段

触发

<?php echo '<td><a href="delete.php?staff_id=' . $row['staff_id'] . '"><input type="button" onclick="confirmDelete(event)" value="delete"></a></td>'; ?></td>

删除确认代码段

function confirmDelete(e) {
 if(confirm('Are you sure you want to delete this Record?'))
   alert('Record Deleted !');
 else {
  alert('Cancelled !');
  e.preventDefault();
 }
}
</script>

我认为IE 8不喜欢链接内的<输入>标签。您可以将 onclick 处理程序添加到""标记中:

<?php echo '<td><a href="delete.php?staff_id=' . $row['staff_id'] . '" onclick="confirmDelete(event)">Delete</a></td>'; ?>

顺便说一句:你有两个",一个在php块之外。

编辑

第二个想法:当删除操作更改应用程序的状态时,最好"发布"数据。所以更好的方法是:

<form action="delete.php" method="post" onsubmit="confirmDelete(event)">
<div>
    <input type="hidden" name="staff_id" value="<?php echo $row['staff_id']; ?>" />
    <input type="submit" name="submit" value="Delete" />
</div>
</form>

在 PHP 中,您可以通过 $_POST['staff_id'] 访问staff_id。

编辑2:

更新了javascript,两种方法(链接和按钮):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>test</title>
<script type="text/javascript">
//<![CDATA[
function confirmDelete() {
    if (!confirm('Delete?')) {
        return false;
    }
    return true;
}
//]]>
</script>
</head>
<body>
    <h1>Version 1 (link)</h1>
    <div>
        <a href="delete.php?staff_id=1" onclick="return confirmDelete()">Delete</a>
    </div>
    <h1>Version 2 (button)</h1>
    <form action="delete.php" method="post" onsubmit="return confirmDelete()">
    <div>
        <input type="hidden" name="staff_id" value="<?php echo $row['staff_id']; ?>" />
        <input type="submit" name="submit" value="Delete" />
    </div>
    </form>
</body>
</html>

在IE 7-10和FF中测试。

问候

发生这种情况是因为 FF 和 IE 以不同的方式冒泡点击事件。

问题是您的按钮位于标签内,该标签具有自己的点击处理程序。

你应该尝试这样的事情:

<a href="delete.php?id=1" onclick="confirmDelete(event)">delete</a>

甚至更简单:

<a href="delete.php?id=1" onclick="return confirm('Are you sure?')">delete</a>

这将在所有浏览器中以相同的方式工作。

当我们从数据库中删除任何内容时,您也可以使用两次确认消息。这是代码

function confirmDelete() {
    var cont = false;
    cont = confirm('Warning! Delete this membership level?')
    if (!cont) {
        return false;
    }
    cont = confirm('Last Warning! Are you really sure?'nDeleting this membership level cannot be undone!');
    if (!cont) {
        return false;
    }
    return true;
}
<a href="action=delete_level&id=<?php echo $member_level; ?>" onclick="return confirmDelete()">Delete</a>

谢谢。。