Javascript 如何动态更改引导模态主体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34103136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:55:50  来源:igfitidea点击:

How to dynamically change bootstrap modal body

javascriptjquerytwitter-bootstrap

提问by VTodorov

So I have Bootstrap modal defined as:

所以我将 Bootstrap 模式定义为:

   <div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Error</h4>
                </div>
                <div class="modal-body">
                    <p> </p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

And I want to edit the empty body with jscript. Im a beginner in Javascript so I'll appreciate the simplest answer.

我想用 jscript 编辑空体。我是 Javascript 的初学者,所以我会欣赏最简单的答案。

回答by Raman

Please have a look at thisfiddle.

请看看这个小提琴。

Here is the code:

这是代码:

$(function(){
  $('.custom-modal').click(function(e){
    e.preventDefault();
    var mymodal = $('#myModal');
    mymodal.find('.modal-body').text('hello');
    mymodal.modal('show');

  });
})

回答by Ajay Makwana

Try this on:

试试这个:

$('.modalShow').click(function(event){
    event.preventDefault();
    var e = $(this);
    var title = e.data('title');
    var body = e.data('value');
    $("#myModal").modal("show");
    $('#modal-title').html(title);
    $('#modal-body').html(body);
});

回答by Manoj Salvi

the simplest solution is - you can use javascript's innerHTMLto change html of your modal body like this -

最简单的解决方案是-您可以使用 javascriptinnerHTML来像这样更改模态主体的 html-

//get your modal body by class and change its html
document.getElementByClass("modal-body").innerHTML = "<p>some text</p>";