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

标签 javascript internet-explorer-8

我有一个 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 不喜欢链接内的 <input> 标记。您可以将 onclick 处理程序添加到“”标记:

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

顺便说一句:你有两个“”,一个在 php block 之外。

编辑

第二个想法:由于删除操作会更改应用程序的状态,因此“POST”数据是一个很好的做法。所以更好的方法是:

<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 中测试。

问候,

关于Javascript 警报单击删除在 IE8 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19088768/

相关文章:

javascript - 我应该使用什么进行 AngularJS E2E 测试? Protractor 或 Angular Scenario Runner 或 Karma

javascript - 在这种情况下,将对象引用传递到 Date.toLocaleString() 中,TypeScript linter 到底要求什么?

javascript - 未调用自动完成 formatItem, Firebug 中也没有任何错误

css - 动态加载的 css 不适用于 IE8 中动态加载的 js View

html - 字体大小不适用于 IE8

javascript - 使用try catch的主要原因是什么?

Javascript 文件大小,这有什么区别吗?

javascript - IE8 中的 __proto__ 使用 ko

css - 我使用 box-shadow css 但它在 IE8 中不起作用

javascript - IE8 - JavaScript 下拉 onchange 调用 "role()"导致错误