最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert to Bootbox confirmation - Stack Overflow

programmeradmin2浏览0评论

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.

I would like to do the same thing with 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?

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.

I would like to do the same thing with 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?

Share Improve this question edited Jun 18, 2013 at 16:04 madth3 7,34412 gold badges52 silver badges74 bronze badges asked Jun 7, 2013 at 15:00 OualidOualid 4031 gold badge9 silver badges23 bronze badges 5
  • if (result) document.location.href = this.attr('href');. No else required. – Julian H. Lam Commented Jun 7, 2013 at 15:06
  • When I used this.attr('href'); I get TypeError: this.attr is not a function. When I change to $(this).attr('href'); i get Undefined. Any idea? – Oualid Commented Jun 7, 2013 at 21:09
  • Hm... what if you just tried this.href? – Julian H. Lam Commented Jun 11, 2013 at 16:12
  • 1 I think the issue with @JulianH.Lam's solution is a scope issue. $(this) as the link doesn't exist in the bootbox.confirm() function. I've had a similar issue. My solution was to create a hidden button and a visible button and then in the if(result) { } call $("#hiddenbuttonID).click(). – RandomWebGuy Commented Jun 18, 2013 at 17:25
  • I found how to solve the problem and you can find answer here: stackoverflow.com/questions/11379794/… – lonecat Commented Sep 14, 2013 at 18:38
Add a comment  | 

2 Answers 2

Reset to default 19

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

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

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();
        }
    });
});
发布评论

评论列表(0)

  1. 暂无评论