JavaScript确认取消按钮不停止JavaScript

JavaScript确认取消按钮不停止JavaScript,javascript,confirm,dialog,Javascript,Confirm,Dialog,我有一个删除按钮,它与我所拥有的页面上的一些评论相关联。当你点击删除按钮时,我试图弹出一个确认对话框,询问你是否确定要删除评论。单击“确定”应运行删除注释的功能,单击“取消”不应运行该功能,只需关闭对话框即可 这是我的代码: onclick="confirm('Are you sure that you want to delete this comment?'); commentDelete(1);" 我的问题是:单击“取消”时,删除功能仍在运行。我猜该函数仍在被调用,因为当我单击cance

我有一个删除按钮,它与我所拥有的页面上的一些评论相关联。当你点击删除按钮时,我试图弹出一个确认对话框,询问你是否确定要删除评论。单击“确定”应运行删除注释的功能,单击“取消”不应运行该功能,只需关闭对话框即可

这是我的代码:

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

我的问题是:单击“取消”时,删除功能仍在运行。我猜该函数仍在被调用,因为当我单击cancel时,它只是在JavaScript中向前推进并调用该函数。我怎样才能正确地做到这一点?我知道这可能是一个简单的问题。谢谢你的帮助

您应该返回false以防止默认事件。 这应该起作用:

onclick="confirm('Are you sure that you want to delete this comment?'); commentDelete(1);return false;"
如果,则缺少一个
。在您的版本中,首先您会得到一个问题,然后不管答案如何,您都会调用
commentDelete

您正在处理的是,如果它是
if
语句,它只会返回布尔值true或false

if(confirm('foo')){ alert('bar'); }

可能是因为您在包含javascript的表单中设置了
type=submit

如果单击“取消”不想提交,则应将其设置为“按钮”、“图像”或其他任何选项

<form name="form_name">
    <input type="button" onclick="javascript_prompt()" value="GO"/>
    <input type="hidden" name="new_name" value=""/>
</form>
在head标记中,您可以编写以下代码:

函数getConfirmation()
{
var retVal=confirm(“是否继续?”);
如果(retVal==true)
{
警报(“用户想要继续!”);
返回true;
} 
其他的
{
警报(“用户不想继续!”);
返回false;
}
}
编写此代码后,可以在以下代码中调用此函数:

<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
    CommandName="Edit" Text="Edit" **OnClientClick="getConfirmation()"**>
</asp:LinkButton>

这可能对某人有所帮助

var result = confirm("Are you sure");
return !!result;

是的,
return false
丢失,但这不是OP最大的问题。谢谢您的帮助!我知道这是一些基本的东西+1给你!
function confirmCancel(){
   var msj='Are you sure that you want to delete this comment?';
   if (!confirm(msj)) { 
      return false;
   } else {
      window.location='backcables.php';
   }
}
<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>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
    CommandName="Edit" Text="Edit" **OnClientClick="getConfirmation()"**>
</asp:LinkButton>
var result = confirm("Are you sure");
return !!result;