如何在删除前显示确认信息?
我想要点击删除(这可能是一个button或图像)得到一个确认消息 。 如果用户select“ Ok
”,则删除完成,否则如果单击“ Cancel
”则不会发生任何事情。
当button被点击时,我试图回应这个,但回声的东西使我的input框和文本框失去了他们的风格和devise。
写在button的onclick
事件:
var result = confirm("Want to delete?"); if (result) { //Logic to delete the item }
你可以更好地使用如下
<a href="url_to_delete" onclick="return confirm('Are you sure you want to delete this item?');">Delete</a>
这是如何做到这一点与不显眼的JavaScript和确认消息被保存在HTML中。
<a href="/delete" class="delete" data-confirm="Are you sure to delete this item?">Delete</a>
这是纯净的香草JS,兼容IE 9+:
var deleteLinks = document.querySelectorAll('.delete'); for (var i = 0; i < deleteLinks.length; i++) { deleteLinks[i].addEventListener('click', function(event) { event.preventDefault(); var choice = confirm(this.getAttribute('data-confirm')); if (choice) { window.location.href = this.getAttribute('href'); } }); }
看到它的行动: http : //codepen.io/anon/pen/NqdKZq
function ConfirmDelete() { var x = confirm("Are you sure you want to delete?"); if (x) return true; else return false; } <input type="button" Onclick="ConfirmDelete()">
改善user1697128(因为我不能评论它)
<script> function ConfirmDelete() { var x = confirm("Are you sure you want to delete?"); if (x) return true; else return false; } </script> <button Onclick="return ConfirmDelete();" type="submit" name="actiondelete" value="1"><img src="images/action_delete.png" alt="Delete"></button>
将取消表单提交如果取消按下
尝试这个。 它适用于我
<a href="delete_methode_link" onclick="return confirm('Are you sure you want to Remove?');">Remove</a>
<form onsubmit="return confirm('Are you sure?');" />
适用于表单。 表单特定问题: JavaScript表单提交 – 确认或取消提交对话框
如果您对使用css格式的快速漂亮解决scheme感兴趣,可以使用SweetAlert
$(function(){ $(".delete").click(function(){ swal({ title: "Are you sure?", text: "You will not be able to recover this imaginary file!", type: "warning", showCancelButton: true, confirmButtonColor: "#DD6B55", confirmButtonText: "Yes, delete it!", closeOnConfirm: false }, function(isConfirmed){ if(isConfirmed) { $(".file").addClass("isDeleted"); swal("Deleted!", "Your imaginary file has been deleted.", "success"); } } ); }); });
html { zoom: 0.7 } /* little "hack" to make example visible in stackoverflow snippet preview */ body > p { font-size: 32px } .delete { cursor: pointer; color: #00A } .isDeleted { text-decoration:line-through }
<script src="ajax/jQuery/jquery-2.1.3.min.js"></script> <script src="http://t4t5.github.io/sweetalert/dist/sweetalert-dev.js"></script> <link rel="stylesheet" href="http://t4t5.github.io/sweetalert/dist/sweetalert.css"> <p class="file">File 1 <span class="delete">(delete)</span></p>
HTML:
<a href="#" class="delete" data-confirm="Are you sure to delete this item?">Delete</a>
使用jQuery:
$('.delete').on("click", function (e) { e.preventDefault(); var choice = confirm($(this).attr('data-confirm')); if (choice) { window.location.href = $(this).attr('href'); } });
实践
<form name=myform> <input type=button value="Try it now" onClick="if(confirm('Format the hard disk?')) alert('You are very brave!'); else alert('A wise decision!')"> </form>
网站原文:
function del_confirm(msg,url) { if(confirm(msg)) { window.location.href=url } else { false; } } <a onclick="del_confirm('Are you Sure want to delete this record?','<filename>.php?action=delete&id=<?<id> >')"href="#"></a>
我知道这是旧的,但我需要一个答案,而不是这些,但alpesh的答案为我工作,并希望与可能有同样的问题的人分享。
<script> function confirmDelete(url) { if (confirm("Are you sure you want to delete this?")) { window.open(url); } else { false; } } </script>
正常版本:
<input type="button" name="delete" value="Delete" onClick="confirmDelete('delete.php?id=123&title=Hello')">
我的PHP版本:
$deleteUrl = "delete.php?id=" .$id. "&title=" .$title; echo "<input type=\"button\" name=\"delete\" value=\"Delete\" onClick=\"confirmDelete('" .$deleteUrl. "')\"/>";
这可能不是公开的做法的正确方法,但是这在我的私人网站上工作。 🙂
设置一个确认消息,当你删除的东西在PHP和MySQL …
使用这个脚本代码:
<script> function Conform_Delete() { return conform("Are You Sure Want to Delete?"); } </script>
使用这个html代码:
<a onclick="return Conform_Delete()" href="#">delete</a>
<a href="javascript:;" onClick="if(confirm('Are you sure you want to delete this product')){del_product(id);}else{ }" class="btn btn-xs btn-danger btn-delete" title="Del Product">Delete Product</a> <!-- language: lang-js --> <script> function del_product(id){ $('.process').css('display','block'); $('.process').html('<img src=".http://img.dovov.comloading.gif">'); $.ajax({ 'url':'./process.php?action=del_product&id='+id, 'type':"post", success: function(result){ info=JSON.parse(result); if(result.status==1){ setTimeout(function(){ $('.process').hide(); $('.tr_'+id).hide(); },3000); setTimeout(function(){ $('.process').html(result.notice); },1000); } else if(result.status==0){ setTimeout(function(){ $('.process').hide(); },3000); setTimeout(function(){ $('.process').html(result.notice); },1000); } } }); } </script>
使用jQuery:
$(".delete-link").on("click", null, function(){ return confirm("Are you sure?"); });
<SCRIPT LANGUAGE="javascript"> function Del() { var r=confirm("Are you sure?") if(r==true){return href;}else{return false;} } </SCRIPT>
你的链接:
<a href='edit_post.php?id=$myrow[id]'> Delete</a>
onclick处理函数应该在函数调用后返回false。 例如。
onclick="ConfirmDelete(); return false;">
我认为最简单的不引人注目的解决办法是:
链接:
<a href="http://link_to_go_to_on_success" class="delete">Delete</a>
使用Javascript:
$('.delete').click(function () { return confirm("Are you sure?"); });
<a href="javascript:;" onClick="if(confirm('Are you sure you want to delete this product')){del_product(id);}else{ }" class="btn btn-xs btn-danger btn-delete" title="Del Product">Delete Product</a> function del_product(id){ $('.process').css('display','block'); $('.process').html('<img src=".http://img.dovov.comloading.gif">'); $.ajax({ 'url':'./process.php?action=del_product&id='+id, 'type':"post", success: function(result){ info=JSON.parse(result); if(result.status==1){ setTimeout(function(){ $('.process').hide(); $('.tr_'+id).hide(); },3000); setTimeout(function(){ $('.process').html(result.notice); },1000); }else if(result.status==0){ setTimeout(function(){ $('.process').hide(); },3000); setTimeout(function(){ $('.process').html(result.notice); },1000); } } }); }
这里是纯JS使用className和绑定事件的另一个简单的例子。
var eraseable = document.getElementsByClassName("eraseable"); for (var i = 0; i < eraseable.length; i++) { eraseable[i].addEventListener('click', delFunction, false); //bind delFunction on click to eraseables } function delFunction(){ var msg = confirm("Are you sure?"); if (msg == true) { this.remove(); //remove the clicked element if confirmed } };
<button class="eraseable"> <img class="eraseable" src="http://zelcs.com/wp-content/uploads/2013/02/stackoverflow-logo-dumpster.jpg" style="width:100px;height:auto;"> Delete me</button> <button class="eraseable"> <img class="eraseable" src="http://zelcs.com/wp-content/uploads/2013/02/stackoverflow-logo-dumpster.jpg" style="width:100px;height:auto;"> Delete me</button> <button class="eraseable"> <img class="eraseable" src="http://zelcs.com/wp-content/uploads/2013/02/stackoverflow-logo-dumpster.jpg" style="width:100px;height:auto;"> Delete me</button>
<script> function deleteItem() { var resp = confirm("Do you want to delete this item???"); if (resp == true) { //do something } else { //do something } } </script>
使用onClick
调用这个函数
对于“删除确认信息”使用:
$.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "Searching.aspx/Delete_Student_Data", data: "{'StudentID': '" + studentID + "'}", dataType: "json", success: function (data) { alert("Delete StudentID Successfully"); return true; }
使用Javascript删除示例的Angularjs
html代码
<button ng-click="ConfirmDelete(single_play.play_id)" type="submit" name="actiondelete" value="1"><img src="images/remove.png" alt="Delete"></button>
“single_play.play_id”是任何的angularjsvariables,假设你想要在删除操作中传递任何参数
应用程序模块内的Angularjs代码
$scope.ConfirmDelete = function(yy) { var x = confirm("Are you sure you want to delete?"); if (x) { // Action for press ok $http({ method : 'POST', url : 'sample.php', headers: {'Content-Type': 'application/x-www-form-urlencoded'}, data: $.param({ delete_play_id : yy}) }).then(function (response) { $scope.message = response.data; }); } else { //Action for cancel return false; } }
var x = confirm("Are you sure you want to send sms?"); if (x) return true; else return false;
function confirmDelete() { var r=confirm("Are you sure you want to delte this image"); if (r==true) { //User Pressed okay. Delete } else { //user pressed cancel. Do nothing } } <img src="deleteicon.png" onclick="confirmDelete()">
您可能想要通过confirmDelete传递一些数据来确定哪些条目将被删除