我想要得到一个确认消息点击删除(这可能是一个按钮或图像)。如果用户选择“Ok”,那么删除就完成了,否则如果点击“Cancel”,什么都不会发生。
当点击按钮时,我尝试回显这个,但是回显的东西使我的输入框和文本框失去了它们的样式和设计。
我想要得到一个确认消息点击删除(这可能是一个按钮或图像)。如果用户选择“Ok”,那么删除就完成了,否则如果点击“Cancel”,什么都不会发生。
当点击按钮时,我尝试回显这个,但是回显的东西使我的输入框和文本框失去了它们的样式和设计。
当前回答
实践
<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
其他回答
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');
}
});
您可以更好地使用以下方法
<a href="url_to_delete" onclick="return confirm('Are you sure you want to delete this item?');">Delete</a>
对于选择选项框,要做到这一点要困难得多。 下面是解决方案:
<select onchange="if (this.value == 'delete' && !confirm('THIS ACTION WILL DELETE IT!\n\nAre you sure?')){this.value=''}">
<option value=''> </option>
<option value="delete">Delete Everything</option>
</select>
如果你对一些快速漂亮的解决方案与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>
当你在PHP和mysql中删除一些东西时,设置一个确认消息…
使用以下脚本代码:
<script>
function Conform_Delete()
{
return conform("Are You Sure Want to Delete?");
}
</script>
使用下面的HTML代码:
<a onclick="return Conform_Delete()" href="#">delete</a>