如何动态更改bootstrap模态体

时间:2015-12-05 09:06:02

标签: javascript jquery twitter-bootstrap

所以我将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>

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

4 个答案:

答案 0 :(得分:9)

请查看this小提琴。

以下是代码:

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

  });
})

答案 1 :(得分:7)

试试这个:

$('.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);
});

答案 2 :(得分:2)

最简单的解决方案是 - 您可以使用javascript&#39; innerHTML更改模态正文的html -

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

答案 3 :(得分:0)

我已经在C#中完成了以下操作。

C#:

System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
        stringBuilder.Append(@"<script language='javascript'>");
        stringBuilder.Append(@"$('#errorModal').find('.modal-body').text('"+ your error message + "');");
        stringBuilder.Append(@"$('#errorModal').modal('show');");
        stringBuilder.Append(@"</script>");
        ClientScript.RegisterStartupScript(this.GetType(), "JSScript", stringBuilder.ToString());

HTML:

    <div class="modal fade" id="errorModal" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Modal title</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>