【问题标题】:How to bring up an okay / cancel dialog box in Javascript? [duplicate]如何在 Javascript 中弹出一个确定/取消对话框? [复制]
【发布时间】:2016-07-03 19:24:21
【问题描述】:

当我点击 StackOverflow 上的“删除帖子”时,它会在屏幕上弹出一个窗口,询问我是要继续还是要取消。

我怎样才能在 Javascript 中做到这一点,以便它可以在计算机和 iPad 上运行?

【问题讨论】:

    标签: javascript dialog


    【解决方案1】:

    可以通过confirm实现,代码如下:

    <button onclick="myFunction()">Try it</button>
    <script>
    function myFunction() {
    confirm("Press a button!");
    }
    </script>
    

    上面的例子很基础,如果你设计了确认框,请查看craftpip's jquery-confirm希望你喜欢。

    【讨论】:

      【解决方案2】:

      您可以为此使用confirm。它将根据用户的操作返回truefalse。示例:

      var result = confirm("Please confirm this action!")
      if(result)
          //The user confirmed the action
      else
          //The user declined the action
      

      【讨论】:

        【解决方案3】:

        正如其他人所说,使用confirm 方法。

        您可以像这样从确认中获取回调:

        confirm("Are you sure you want to do this?", function(result) {
        if(result)
            doSomethingPositive();
        else
            doSomethingNegative();
        });
        

        【讨论】:

        • 1) 不要重复其他人已经给出的答案,除非你有什么要补充的。 2)这不是你使用confirm()的方式
        • @yourmom 这种方法有什么问题?当我创建答案时,没有回调 inovked 的示例
        • confirm 方法没有任何回调。它只是根据用户操作返回 true 或 false。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多