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

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


当前回答

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

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

其他回答

它可以简化为:

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

HTML

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

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

<form onsubmit="return confirm('Are you sure?');" />

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

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

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