我想要得到一个确认消息点击删除(这可能是一个按钮或图像)。如果用户选择“Ok”,那么删除就完成了,否则如果点击“Cancel”,什么都不会发生。

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


当前回答

对于“删除确认消息”使用:

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

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

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

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

在按钮的onclick事件中写入以下内容:

var result = confirm("Want to delete?");
if (result) {
    //Logic to delete the item
}

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