javascript - 在php中确认后删除
问题描述
我有一个联系表格。现在当我需要删除一条记录时,我需要一个确认框来要求用户确认。我已经做到了,到目前为止一切都很好。OK
但是和Cancel
按钮的记录都被删除了。
我的代码如下:
<td><a href="edit.php?id=<?php $_SESSION["name"] = ""; $_REQUEST["id"]=$row["first_name"]; echo $row["first_name"]; $_SESSION["name"]=$row["first_name"]; ?>">Edit</a> / <a href="delete.php?id=<?php echo $row["first_name"]; ?>" onclick="check()"> Delete </a></td>
<script>
function check(){
if(confirm("Are you sure you want to delete?")){
window.location.href = "delete.php?id=<?php echo $row["first_name"]; ?>";
return true;
}
else{
header("Location: http://localhost/test/display.php?show_details=Show+Details");
return false;
}
}
</script>
OK
只有单击确认框中的按钮并单击返回display.php
(同一页面)后,我该怎么做才能删除记录Cancel
?
它应该delete.php
仅在单击时导航到OK
并停留在display.php
单击时Cancel
。
我是 PHP 新手。请帮助...
解决方案
HTML 锚点有一个默认的点击操作:打开一个链接。现在,如果您在 JavaScript 中添加另一个点击处理程序,您将不得不阻止该元素的默认操作,从而阻止打开链接。
为了防止内联事件处理程序(与在 JSonclick
中使用相比使用属性设置的事件处理程序)中的默认事件操作,addEventListener()
您必须false
在该事件处理程序中返回。
以下两个片段修复了该问题,并且也重新使用了href
-attribute,因此不得再次在 JS 中对其进行硬编码。
变体1:
<p>
<a href="edit.php?id=123">Edit</a>
/
<a href="delete.php?id=123" onclick="return check(this)">Delete</a>
</p>
<script>
function check(anchor) {
/* Only open the delete-link, if the confirm returns TRUE */
if (confirm('Are you sure you want to delete?')) {
window.location.href = anchor.href;
}
/* Return FALSE to prevent the default link action */
return false;
}
</script>
变体 2:
<p>
<a href="edit.php?id=123">Edit</a>
/
<!-- Return FALSE to prevent the default link action -->
<a href="delete.php?id=123" onclick="check(this); return false;">Delete</a>
</p>
<script>
function check(anchor) {
/* Only open the delete-link, if the confirm returns TRUE */
if (confirm('Are you sure you want to delete?')) {
window.location.href = anchor.href;
}
}
</script>
变体 3A:
不返回 false,而是使用event.preventDefault()
. (灵感来自@ths 的评论)
<p>
<a href="edit.php?id=123">Edit</a>
/
<a href="delete.php?id=123" onclick="check(event, this)">Delete</a>
</p>
<script>
function check(event, anchor) {
/* Prevent the default link action */
event.preventDefault();
/* Only open the delete-link, if the confirm returns TRUE */
if (confirm('Are you sure you want to delete?')) {
window.location.href = anchor.href;
}
}
</script>
变体 3B:
不返回 false,而是使用event.preventDefault()
. (灵感来自@ths 的评论)
<p>
<a href="edit.php?id=123">Edit</a>
/
<a href="delete.php?id=123" onclick="event.preventDefault(); check(this);">Delete</a>
</p>
<script>
function check(anchor) {
/* Only open the delete-link, if the confirm returns TRUE */
if (confirm('Are you sure you want to delete?')) {
window.location.href = anchor.href;
}
}
</script>
推荐阅读
- javascript - jQuery show/hide options if first one selected, shows second selected option by default
- excel - Change the grouping level of a newly inserted row (Excel)
- ffmpeg - 如何使用 GPU 使用 ffmpeg.autogen 解码 MP4 文件?
- eclipse - why is my debug break point not hitting on my lamda function (java)?
- html - How to hide specific element of class and show other elements of same class using css
- php - 如何在laravel中选择嵌套关系中的最低项目
- python - Visual Studio 代码 - 禁用 jupyter
- java - 无法转换 ArrayList
> 到数组列表 > - android - 将 textView 中的第二行对齐到中心,compoundDrawable 对齐到第一行
- bioinformatics - 如何设置不同颜色的 Cytoscape 网络节点中心?