JavaScript confirm cancel button not stopping 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"
您缺少 if
.在您的版本中,首先您会收到一个问题,然后无论答案如何,您都会调用 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JavaScript 确认取消按钮不停止 JavaScript
基础教程推荐
- 直接将值设置为滑块 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01