JavaScript确认取消按钮不停止JavaScript

JavaScript确认取消按钮不停止JavaScript

本文介绍了JavaScript确认取消按钮不停止JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个删除按钮,绑定在我有一些页面上的一些评论。当您单击删除按钮时,我尝试得到一个确认对话框弹出询问是否确定要删除评论。单击确定应该运行该功能删除注释,单击取消不应该运行该功能,但只需关闭对话框。

I have a delete button that is tied to some comments on a page i have. When you click the delete button i am trying to get a confirm dialog box to pop up asking if you are sure you want to delete the comment. Clicking OK should run the function to delete the comment and clicking cancel should not run the function but simply close the dialog box.

这是我的代码:

onclick="confirm('Are you sure that you want to delete this comment?'); commentDelete(1);"

我的问题:当我点击取消删除功能仍然运行。我的猜测是,该函数仍然被调用,因为当我点击取消它只是在JavaScript中前进并调用该函数。我如何正确地完成这个?我知道这可能是一个简单的问题。感谢任何帮助!

My problem: When i click cancel the delete function still runs. My guess is that the function is still getting called because when i click cancel it just is stepping forward in the JavaScript and calling the function. How can i accomplish this correctly? I know this is probably a simple problem. Thanks for any help!

推荐答案

onclick="if (confirm('Are you...?')) commentDelete(1); return false"

你缺少一个如果。在你的版本中,首先你会得到一个问题,然后不管答案如何,你调用 commentDelete

You are missing an if. In your version, first you get a question, and then regardless of the answer, you call commentDelete.

这篇关于JavaScript确认取消按钮不停止JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:06