Javascript 在删除表格的一行之前显示一个确认框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26254910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
show a confirmation box before deleting a row of the table
提问by user3774056
I have a table whose view is something like this
我有一张桌子,它的视图是这样的
ID NAME LOCATION DELETE
1 sam US delete
I have a statement from the table that deletes the given row
我从表中删除了给定行的语句
echo "<td><a href=\"delete_members.php?id=".$row['id']."\">Delete</a></td>";
It redirects to delete_members.php page and the row gets deleted, however i wish to display an alert box that makes sure that the user wants to delete the row or not. for it i have a code
它重定向到 delete_members.php 页面并删除该行,但是我希望显示一个警报框,以确保用户是否要删除该行。因为它我有一个代码
<button onclick="myFunction()">Delete</button>
<script>
function myFunction() {
var x;
if (confirm("Press a button!") == true) {
window.location="yourphppage.php"; // not sure which link should be placed here
return true;
} else {
window.location="index.php";
return true;
}
document.getElementById("demo").innerHTML = x;
}
</script>
code for the delete_members.php page
delete_members.php 页面的代码
<?php
include('admin_session.php');
$con=mysqli_connect("abc.com","abc","abc","abc");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$id = $_GET['id'];
mysqli_query($con,"DELETE FROM members WHERE id='".$id."'");
mysqli_close($con);
header("Location: admin_member_list.php");
?>
However, i am unable to use the confirmation script with the statement in a proper way. i want that the alert box gets popped up(i.e the script runs) after i click on the edit button that is present on the first statement given above and then if the user confirms it should run the delete_members.php script. would appreciate if someone could guide me
但是,我无法以正确的方式使用带有语句的确认脚本。我希望在我单击上面给出的第一个语句中存在的编辑按钮后弹出警报框(即脚本运行),然后如果用户确认它应该运行 delete_members.php 脚本。如果有人能指导我,我将不胜感激
回答by Reinder Wit
In the most simple way:
以最简单的方式:
echo "<a onclick=\"return confirm('Delete this record?')\" href=\"delete_members.php?id=".$row['id']."\">delete</a>";
you could also do this in an unobtrusive manner, by adding a class to the links and selecting them all at once using something like jQuery and then bind the confirmation logic to the onclick event. Something like this:
您还可以通过向链接添加一个类并使用 jQuery 之类的东西一次选择它们,然后将确认逻辑绑定到 onclick 事件,以一种不显眼的方式执行此操作。像这样的东西:
$('a.delete').on('click', function() {
var choice = confirm('Do you really want to delete this record?');
if(choice === true) {
return true;
}
return false;
});
回答by Yash Sodha
echo "<a href='delete_members.php?id={$row['id']}' onclick=\"return confirm('Do you really want to delete this?')\">DELETE</a>";
回答by Mohd. Shaukat Ali
You can try this. Hope it will work for you..
你可以试试这个。希望它对你有用..
Add a 'delete' class to anchor tag.
向锚标记添加“删除”类。
echo "<td><a href=\"delete_members.php?id=".$row['id']."\" class=\"delete\">Delete</a></td>";
Then bind delete function:
然后绑定删除函数:
$('.delete').on('click', myFunction);
回答by Choco
I think this will be useful please look at this
我认为这会很有用,请看这个
$id=$row['id'];
$id=$row['id'];
If you want to pass id you can use $id or else just call function without params
如果你想传递 id 你可以使用 $id 或者只调用没有参数的函数
echo "<a href='#' onclick='deletemember($id)'>Delete</a>";
<script>
function deletemember(id)
{
var r = confirm("Are you sure you want to delete");
if(r == true)
{
$.ajax({
type:"GET",
url:"delete_members.php",
data: ({id:id}),
success: function(data)
{
window.location="yourphppage.php";
}
});
}
else
{
window.location="index.php";
}
}
</script>

