【问题标题】:JavaScript confirm cancel button not stopping JavaScriptJavaScript 确认取消按钮不停止 JavaScript
【发布时间】:2011-08-15 15:40:21
【问题描述】:

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

这是我的代码:

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

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

【问题讨论】:

    标签: javascript confirm dialog


    【解决方案1】:

    您应该返回 false 以阻止默认事件。 这应该有效:

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

    【讨论】:

    • 没错,return false 不见了,但这不是 OP 最大的问题。
    【解决方案2】:
    onclick="if (confirm('Are you...?')) commentDelete(1); return false"
    

    您缺少if。在你的版本中,首先你会得到一个问题,然后不管答案如何,你都会打电话给commentDelete

    【讨论】:

    • 感谢您的帮助!我知道这是基本的东西。为你 +1!
    【解决方案3】:

    您正在处理 confirm 是否是 if 语句,它只返回一个布尔值 true 或 false。

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

    【讨论】:

      【解决方案4】:

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

      如果你不想在点击取消时提交,你应该将它设置为按钮或图像或其他任何东西

      <form name="form_name">
          <input type="button" onclick="javascript_prompt()" value="GO"/>
          <input type="hidden" name="new_name" value=""/>
      </form>
      

      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;
          }
      }
      

      【讨论】:

        【解决方案5】:
        function confirmCancel(){
           var msj='Are you sure that you want to delete this comment?';
           if (!confirm(msj)) { 
              return false;
           } else {
              window.location='backcables.php';
           }
        }
        

        【讨论】:

          【解决方案6】:

          在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>
          

          写完这段代码后,你可以在下面的代码中调用这个函数:

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

          【讨论】:

            【解决方案7】:

            希望这对某人有帮助。

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2017-12-07
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2010-10-23
              • 2013-02-04
              • 1970-01-01
              相关资源
              最近更新 更多