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

bootbox.js -> Show button in bootbox.dialog() only if condition is true

我使用 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();
}

但是没有成功。

任何想法表示赞赏!

你好

你可以像这样在 bootbox 中使用 className :

var condition = false;
var displayButton="show";
if(condition){
    displayButton="hide";
}

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

});

工作示例:https://jsfiddle.net/py6wbmvp/

只需修改传递给引导箱的按钮对象,就像这样

  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/