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

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


当前回答

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

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

其他回答

<script>
function deleteItem()
{
   var resp = confirm("Do you want to delete this item???");
   if (resp == true) {
      //do something
   } 
   else {
      //do something
   }
}
</script>

使用onClick调用此函数

试试这个。这对我很有用

 <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?");
});

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