【问题标题】:Bootbox javascript confirm dialog boxBootbox javascript 确认对话框
【发布时间】:2014-08-24 07:00:11
【问题描述】:

这行代码有问题:

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

还是不行。

【问题讨论】:

标签: javascript jquery bootbox


【解决方案1】:

只要这样做:

假设你有删除按钮

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

delete-it类的点击事件下写this

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

根据下面的cmets:

使用onclick 被认为是不好的做法。如果您有 $id 值,请像这样将其包装在数据属性上

&lt;a href='#' data-id=' {$id }' class='delete-it'&gt;Delete&lt;/a&gt;

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

【讨论】:

  • 我没有删除按钮。只有一个回显“Delete”;我该如何应用它。?
  • 使用onclick是不好的做法,您可以将锚标记更改为&lt;a href='#' class='delete-it'&gt;Delete&lt;/a&gt;,然后使用我上面提供的功能。
  • 你的意思是:?
  • ^ 是的,在加载 bootbox 和 jquery 库之后放置这个脚本。
  • 非常感谢...点击的响应速度比另一个快。在删除数据之前需要单击两次。
【解决方案2】:

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

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

您将获得resultTrueFalse,具体取决于点击YesNo 中的Dialog Box

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

【讨论】:

    【解决方案3】:

    试试这个:

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

    【讨论】:

    • 仍然无法正常工作。 :( 我是新手,还在尝试中
    • 已经开始工作了,谢谢 S.Pols!我只是申请:
    猜你喜欢
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多