JavaScript 确认取消按钮不会停止 JavaScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5834983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript confirm cancel button not stopping JavaScript
提问by RyanPitts
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.
我有一个删除按钮,它与我拥有的页面上的一些评论相关联。当您单击删除按钮时,我试图弹出一个确认对话框,询问您是否确定要删除评论。单击确定应运行该函数以删除注释,单击取消不应运行该函数而只是关闭对话框。
This is my code:
这是我的代码:
onclick="confirm('Are you sure that you want to delete this comment?'); commentDelete(1);"
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!
我的问题:当我单击取消时,删除功能仍在运行。我的猜测是该函数仍在被调用,因为当我单击取消时,它只是在 JavaScript 中前进并调用该函数。我怎样才能正确地做到这一点?我知道这可能是一个简单的问题。谢谢你的帮助!
回答by Amadan
onclick="if (confirm('Are you...?')) commentDelete(1); return false"
You are missing an if
. In your version, first you get a question, and then regardless of the answer, you call commentDelete
.
你缺少一个if
. 在您的版本中,首先您会收到一个问题,然后不管答案如何,您都调用commentDelete
.
回答by djyJK
In the head tag you can write following code:
在 head 标签中,您可以编写以下代码:
<script language="javascript" type="text/javascript">
function getConfirmation()
{
var retVal = confirm("Do you want to continue ?");
if (retVal == true)
{
alert("User wants to continue!");
return true;
}
else
{
alert("User does not want to continue!");
return false;
}
}
</script>
After writing this code you can call this function in the following code:
编写此代码后,您可以在以下代码中调用此函数:
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit" **OnClientClick="getConfirmation()"**>
</asp:LinkButton>
回答by epascarello
回答by Lucas
function confirmCancel(){
var msj='Are you sure that you want to delete this comment?';
if (!confirm(msj)) {
return false;
} else {
window.location='backcables.php';
}
}
回答by vikas0713
May this help someone.
可能这对某人有帮助。
var result = confirm("Are you sure");
return !!result;
回答by Martin
Maybe it's because you set the type=submit
in the form that contains the javascript.
也许是因为您type=submit
在包含 javascript 的表单中设置了。
You should set it to button or image or whatever if you don't want to be submitted if clicked cancel
如果您不想在单击取消时提交,则应将其设置为按钮或图像或其他任何内容
<form name="form_name">
<input type="button" onclick="javascript_prompt()" value="GO"/>
<input type="hidden" name="new_name" value=""/>
</form>
example of javascript prompt:
javascript 提示示例:
function javascript_prompt(){
var name = prompt("Tell me your name.", "");
if (name != "" && name != null){
document.form_name.new_name.value = name;
document.form_name.submit();
}else{
return false;
}
}
回答by Felipe
You should return false to prevent the default event. This should work:
您应该返回 false 以防止默认事件。这应该有效:
onclick="confirm('Are you sure that you want to delete this comment?'); commentDelete(1);return false;"