Bootbox javascript确认对话框

时间:2014-08-24 07:00:11

标签: javascript jquery bootbox

我对这行代码有疑问:

<script type='text/javascript'>
function delete_user( id ){

    var answer = confirm('Are you sure?');

    //if user clicked ok
    if ( answer ){
        //redirect to url with action as delete and id to the record to be deleted
        window.location = 'delete.php?id=' + id;
    }
}
</script>

如何在这里应用bootbox.js。我讨厌旧的JavaScript对话框。我怎么能改变这个?

我已经完成了这一行:

<script src="bootstrap.min.js"></script>

仍然无法正常工作。

3 个答案:

答案 0 :(得分:1)

这样做:

假设您有删除按钮

<button class="delete-it">Delete</delete>

在类delete-it

的点击事件下写下此内容
 $(".delete-it").click(function(){
    bootbox.confirm("Are you sure?", function(result) {
      if(result)
          window.location = 'delete.php?id=' + id;
    }); 
  });

根据以下评论:

使用onclick被视为不良做法。如果你有$id值将它包装在像这样的数据属性

<a href='#' data-id=' {$id }' class='delete-it'>Delete</a>

将JS改为:

 $(".delete-it").click(function(){
    var id = $(this).data('id');
    bootbox.confirm("Are you sure?", function(result) {
      if(result)
          window.location = 'delete.php?id=' + id;
    }); 
  });

答案 1 :(得分:0)

您可以在confirm中显示bootbox对话框,如下所示

bootbox.confirm("Are you sure you want to Delete?", function(result) {
  if(result)
      window.location = 'delete.php?id=' + id;
});

根据点击result中的TrueFalse,您将Yes作为NoDialog Box获取。

有关bootbox确认对话框的详细信息,您可以查看here

答案 2 :(得分:-1)

试试这个:

<!-- JS dependencies -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="bootstrap.min.js"></script>

<!-- bootbox code -->
<script src="bootbox.min.js"></script>

<script type='text/javascript'>

function delete_user( id )
{
    bootbox.confirm("Are you sure?", function(result) 
    {
        answer = result;
    }); 

    //if user clicked ok
    if ( answer ){
        //redirect to url with action as delete and id to the record to be deleted
        window.location = 'delete.php?id=' + id;
    }
}
</script>