javascript - bootbox.js -> 仅当条件为 true 时才在 bootbox.dialog() 中显示按钮

标签 javascript jquery bootbox

我使用 bootbox 创建一个对话框,但我希望某些按钮仅在某些条件下显示。

我搜索了很多,但没有发现任何有用的信息..

我这样定义引导箱:

bootbox.dialog({
   title: "title",
   message: "text",
   buttons: {
      btn1: {
         label: "Button 1",
         callback: function () {
            /* do something */                                
         }
      },
      btn2: {
         label: "Button 2",
         callback: function () {
            /* do something */                                
         }
      }       
});

如何使第二个按钮仅出现 if(condition == true)

之后我也尝试像这样删除按钮:

bootbox.dialog({...})
if(!condition) {
   $('[data-bb-handler="btn2"]').remove();
}

但没有成功。

任何想法表示赞赏!

问候

最佳答案

只需修改传递到引导箱的按钮对象,如下所示

  var buttons = {
    btn1: {
      label: "Button 1",
      callback: function() {
        /* do something */
      }
    },
  }

  // change here !!!    
  if (false)
    buttons.btn2 = {
      label: "Button 2",
      callback: function() {
        /* do something */
      }
    }

  bootbox.dialog({
    title: "title",
    message: "text",
    buttons: buttons
  });
<小时/>

fiddle - http://jsfiddle.net/7x5h91v2/

关于javascript - bootbox.js -> 仅当条件为 true 时才在 bootbox.dialog() 中显示按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36478641/

相关文章:

twitter-bootstrap - Bootstrap 和 Bootstrap 箱 : set dialog box center of the screen

javascript - 显示确认框时禁用按键

jquery - 将一个元素的文本颜色设置为另一元素的背景颜色

jquery - CSS :hover does not work after jQuery css change

javascript - Angular Promise.all 在超时事件中不起作用

javascript - 动态添加文本框不起作用

jquery - 用动画填充颜色

javascript - 如何隐藏 bootbox.js 模态

javascript - 我的附加 SDK 内容脚本如何与网站页面脚本交互?

javascript - 如何将字符串从 django 传递到 javascript