Php 如何使用确认提示窗口取消删除操作?
在我单击警报窗口中的“取消”后,删除仍在进行! 我怎样才能解决这个问题? 我正在使用MySQL数据库。 工作测试待办事项列表: 删除下面的代码,然后索引页面代码 下面的新代码片段:ajax给了我问题吗?Php 如何使用确认提示窗口取消删除操作?,php,mysql,Php,Mysql,在我单击警报窗口中的“取消”后,删除仍在进行! 我怎样才能解决这个问题? 我正在使用MySQL数据库。 工作测试待办事项列表: 删除下面的代码,然后索引页面代码 下面的新代码片段:ajax给了我问题吗? // Delete entry $('a.deleteEntryAnchor').click(function() { var thisparam = $(this); thisparam.parent().parent().find('p').text('Please hol
// Delete entry
$('a.deleteEntryAnchor').click(function() {
var thisparam = $(this);
thisparam.parent().parent().find('p').text('Please hold your onions.... ☺');
$.ajax({
type: "GET",
url: thisparam.attr('href'),
success: function() {
thisparam.parent().parent().fadeOut('fast');
}
});
return false;
});
-
添加新条目
头衔
描述
//在加载DOM时执行所有这些操作
$(函数(){
//获取所有删除链接(注意我在HTML中给它们的类)
$(“a.deleteEntryAnchor”)。单击(函数(){
//基本上,如果确认为真(按下OK按钮),则
//允许单击事件继续,链接将
//随后-但是,如果按下取消按钮,单击事件将在此停止。
返回确认(“您确定要删除此项吗?”);
});
});
$(function(){}
仅防止Java脚本在DOM
完成加载之前尝试执行操作
在大页面上,完全有可能用户点击链接的速度太快,以至于没有加载Javascript。因此,浏览器正在加载页面的一半,用户发出了转到其他链接的命令,因此它通过重定向用户来做正确的事,而不是完成加载当前页面
您需要在锚点(最安全的)的onclick属性上返回false,或者在jQuery click函数中返回
e.preventDefault()
。基本上,您的js就是问题所在,需要一个preventDefault,我想,它并没有向任何停止表单post的函数返回任何内容
$(“a.deleteEntryAnchor”)。单击(函数(e){
如果(!确认(“是否确实要删除此?”)){
e、 预防默认值();
}
});
require 'db.php';
$db = new Db();
$response = $db->delete_by_id($_GET['id']);
header("Location: index.php");
<div id="container">
<header><img src="/todo/images/heading-trans.gif"></header>
<ul id="tabs">
<li id="todo_tab" class="selected">
<a href="#">To-Do</a>
</li>
</ul>
<div id="main">
<div id="todo">
<?php
require 'db.php';
$db = new Db();
$query = "SELECT * FROM todo ORDER BY id asc";
$results = $db->mysql->query($query);
if($results->num_rows) {
while($row = $results->fetch_object()) {
$title = $row->title;
$description = $row->description;
$id = $row->id;
echo '<div class="item">';
$data = <<<EOD
<h4>$title</h4>
<p>$description</p>
<input type="hidden" name="id" id="id" value="$id" />
<div class="options">
<a class="deleteEntryAnchor" href="delete.php?id=$id">Delete</a>
<a class="editEntry" href="#">Edit</a>
<a class="save-button" href="index.php">Save</a>
</div>
EOD;
echo $data;
echo '</div>';
} // end while
}
else {
echo "<p>There are zero items. Add one now! </p>";
}
?>
</div><!--end todo-->
<div id="addNewEntry">
<h2>Add New Entry</h2>
<form action="addItem.php" method="post">
<p><label for="title"> Title</label>
<input type="text" name="title" id="title" class="input" required/></p>
<p><label for="description"> Description</label>
<textarea name="description" id="description" required></textarea></p>
<p><input type="submit" name="addEntry" id="addEntry" value="Add New Entry" /></p>
</form>
</div><!--end addNewEntry-->
</div><!--end main-->
</div><!--end container-->
<script>
//Do all this when the DOM is loaded
$(function() {
//get all delete links (Note the class I gave them in the HTML)
$("a.deleteEntryAnchor").click(function() {
//Basically, if confirm is true (OK button is pressed), then
//the click event is permitted to continue, and the link will
//be followed - however, if the cancel is pressed, the click event will be stopped here.
return confirm("Are you sure you want to delete this?");
});
});
</script>