Bootbox - 如何动态更改消息内容?

3
我想知道是否有办法动态更新bootbox模态框的内容。
例子:
bootbox.dialog({
        message: "Hi there",
        title: "My title",
        buttons: {
            main: {
                label: "dismiss",
                className: "btn-primary",
            }
        }
    });


    newMessage = "this is a new message"

有没有一种方法可以将“Hi there”替换成新字符串newMessage?
谢谢任何帮助或建议。
3个回答

8

是的,您可以通过添加消息的ID引用来更改bootbox消息。以下是示例代码。

    bootbox.dialog({
       message: "<span id='dynamicMsg'>Hi there</span>",
       title: "My title",
       buttons: {
        main: {
            label: "dismiss",
            className: "btn-primary",
        }
      }
    });

    //Add this line wherever you want to change msg
    $("#dynamicMsg").text("This is dynamic msg");

对我来说,这是更好的解决方案,而不是被接受的方案。 - auron344

2

很简单!创建一个通用函数:

function bootBoxModal(title, message, type) {
    bootbox.dialog({
        message: message,
        title: title,
        alertType: type,
        buttons: {
            main: {
                label: 'Fechar', className: 'btn-default'}
        }
    });
}

现在调用该函数:

bootBoxModal("Title message", 
             "Content your message", 
             "type [alert,danger,warning,success]");

为什么这是被接受的答案?它不会在每次调用该函数时创建另一个对话框,而是更新现有的对话框? - auron344

0
另一种解决方案是直接替换内容,这个例子使用了jQuery。
#jQuery
$('.modal-title').html('New Title');
$('.modal-body').html('New Message');

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接