twitter-bootstrap 转换为 Bootbox 确认

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16987188/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 17:18:35  来源:igfitidea点击:

Convert to Bootbox confirmation

javascriptjquerytwitter-bootstrapbootbox

提问by Oualid

The following code pops up a confirmation windows when the Delete user link is pressed:

当按下删除用户链接时,以下代码会弹出一个确认窗口:

<a href="delete_user.php?id=123" onclick="return confirm('Are you sure?');">Delete user</a>

In this case when the OK button is pressed the link delete_user.php?id=123 will be executed. When the Cancel button is pressed nothing will happened.

在这种情况下,当按下 OK 按钮时,链接 delete_user.php?id=123 将被执行。当按下取消按钮时,什么都不会发生。

I would like to do the same thing with Bootbox.

我想用 Bootbox 做同样的事情。

   <a class="alert" href="list_users.php?id=123">Delete user</a>

    <script src="bootbox.min.js"></script>
        <script>
        $(document).on("click", ".alert", function(e) {
            e.preventDefault();

        bootbox.confirm("Are you sure?", function(result) {

            if (result) {
               // What to do here?
            } else {
               // What to do here?
            }               
        });

        });
    </script>

What to do under if(result) and else statements?

在 if(result) 和 else 语句下做什么?

回答by Sylvain Jacob

This worked for me. Grab the "click" href and use it when you have "result".

这对我有用。获取“点击”href 并在您有“结果”时使用它。

  <script>
        $(document).on("click", ".alert", function(e) {
            var link = $(this).attr("href"); // "get" the intended link in a var
            e.preventDefault();    
            bootbox.confirm("Are you sure?", function(result) {    
                if (result) {
                    document.location.href = link;  // if result, "set" the document location       
                }    
            });
        });
    </script>

回答by Sysem

This works great!

这很好用!

$(".alert").on("click", function (e) {
    // Init
    var self = $(this);
    e.preventDefault();

    // Show Message        
    bootbox.confirm("Are you sure?", function (result) {
        if (result) {                
            self.off("click");
            self.click();
        }
    });
});