如何在删除前显示确认消息?

我想在单击delete时获得确认消息(这可能是一个按钮或图像)。如果用户选择'Ok',则删除完成,否则如果单击'Cancel',则什么也不会发生。

当点击按钮时,我尝试回显这个,但是回显的东西使我的输入框和文本框失去了它们的样式和设计。

863326 次浏览
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传递一些数据,以确定要删除哪个条目

在按钮的onclick事件中写入:

var result = confirm("Want to delete?");
if (result) {
//Logic to delete the item
}
function ConfirmDelete()
{
return confirm("Are you sure you want to delete?");
}




<input type="button" onclick="ConfirmDelete()">

您可以更好地使用以下方法

 <a href="url_to_delete" onclick="return confirm('Are you sure you want to delete this item?');">Delete</a>
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>

使用jQuery:

$(".delete-link").on("click", null, function(){
return confirm("Are you sure?");
});

这就是使用不引人注目的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

改进user1697128(因为我还不能评论它)

<script>
function ConfirmDelete()
{
return confirm("Are you sure you want to delete?");
}
</script>
    

<button Onclick="return ConfirmDelete();" type="submit" name="actiondelete" value="1"><img src="images/action_delete.png" alt="Delete"></button>

如果按“取消”键,会取消提交表格吗

<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;">

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');
}
});

我知道这是旧的,但我需要一个答案,但这些都不是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. "')\"/>";

这可能不是公开的正确方式,但在一个私人网站上,这对我来说是有效的。:)

实践

<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>

Web最初:

http://www.javascripter.net/faq/confirm.htm

试试这个。这对我很有用

 <a href="delete_methode_link" onclick="return confirm('Are you sure you want to Remove?');">Remove</a>

我认为最简单不显眼的解决方案是:

链接:

<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="./images/loading.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);


}
}
});
}
<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="./images/loading.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>

当你在PHP &mysql……

使用以下脚本代码:

<script>
function Conform_Delete()
{
return conform("Are You Sure Want to Delete?");
}
</script>

使用下面的HTML代码:

<a onclick="return Conform_Delete()" href="#">delete</a>
<form onsubmit="return confirm('Are you sure?');" />

适用于表单。特定于形式的问题:JavaScript表单提交-确认或取消提交对话框

下面是纯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;
}

如果你对一些快速漂亮的css格式解决方案感兴趣,你可以使用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
}).then(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="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.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>

Angularjs与Javascript删除示例

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 "是任意angularjs变量,假设你想在删除操作期间传递任何参数

app模块内的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;

我想提供我的方法:

<form action="/route" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="the_token">
<button type="submit" class="btn btn-link" onclick="if (!confirm('Are you sure?')) { return false }"><span>Delete</span></button>
</form>
对于选择选项框,要做到这一点要困难得多。 下面是解决方案:

<select onchange="if (this.value == 'delete' && !confirm('THIS ACTION WILL DELETE IT!\n\nAre you sure?')){this.value=''}">
<option value=''> &nbsp; </option>
<option value="delete">Delete Everything</option>
</select>
var txt;
var r = confirm("Press a button!");
if (r == true) {
txt = "You pressed OK!";
} else {
txt = "You pressed Cancel!";
}

var txt;
var r = confirm("Press a button!");
if (r == true) {
txt = "You pressed OK!";
} else {
txt = "You pressed Cancel!";
}

它非常简单,只有一行代码

<a href="#" title="delete" class="delete" onclick="return confirm('Are you sure you want to delete this item')">Delete</a>

超文本标记语言

<input onclick="return myConfirm();" type="submit" name="deleteYear" class="btn btn-danger" value="Delete">

Javascript

<script>
function myConfirm() {
var result = confirm("Want to delete?");
if (result==true) {
return true;
} else {
return false;
}
}

非常简单

function archiveRemove(any) {
var click = $(any);
var id = click.attr("id");
swal.fire({
title: 'Are you sure !',
text: "?????",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'yes!',
cancelButtonText: 'no'
}).then(function (success) {
if (success) {
$('a[id="' + id + '"]').parents(".archiveItem").submit();
}
})
}

我用这种方法(laravel)-

<form id="delete-\{\{$category->id}}" action="\{\{route('category.destroy',$category->id)}}" style="display: none;" method="POST">
@csrf
@method('DELETE')
</form>


<a href="#" onclick="if (confirm('Are you sure want to delete this item?')) {
event.preventDefault();
document.getElementById('delete-\{\{$category->id}}').submit();
}else{
event.preventDefault();
}">
<i class="fa fa-trash"></i>
</a>


它可以简化为:

<button onclick="return confirm('Are you sure you want to delete?');" />