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

0 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 */                                
         }
      }       
});
Run Code Online (Sandbox Code Playgroud)

我怎样才能让第二个 Button 只出现if(condition == true)

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

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

但没有成功。

任何想法表示赞赏!

格雷茨

pot*_*ngs 5

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

  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
  });
Run Code Online (Sandbox Code Playgroud)

小提琴 - http://jsfiddle.net/7x5h91v2/