如何改变 Bootstrap 4 中模态弹出窗口的内容?
How to vary the contents of a modal popup in Bootstrap 4?
我在 Bootstrap 文档中找到了这个关于如何使用 JQuery 改变模态内容的示例:
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">Open modal for @mdo</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@fat">Open modal for @fat</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@getbootstrap">Open modal for @getbootstrap</button>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">New message</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Recipient:</label>
<input type="text" class="form-control" id="recipient-name">
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Message:</label>
<textarea class="form-control" id="message-text"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Send message</button>
</div>
</div>
</div>
</div>
<script>
$('#exampleModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // Button that triggered the modal
var title = button.data('title') // Extract info from data-* attributes
var recipient = button.data('id') // Extract info from data-* attributes
var modal = $(this)
modal.find('.modal-title').text(title)
modal.find('.modal-body input').val(recipient)
})
</script>
问题是它似乎只更改表单控件(输入或文本区域)的内容。我的目标是更改短信,但我没能做到。有什么想法吗?
谢谢!
我终于找到了解决方案。有人告诉任何地方模态可以被视为任何 html 标签,所以我决定测试使用 Vanilla JS 访问 DOM 像这样:
<script>
$('#exampleModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget)
var title = button.data('title')
var body = button.data('body')
var modal = $(this)
modal.find('.modal-title').text(title)
document.getElementById('body').innerHTML = body
})
</script>
在模态框内,我用这种方式创建了一个 'body' div:
<div class="modal-body">
<div id="body"></div>
</div>
最后打开模态和传递数据的按钮的代码是:
<a class='btn btn-secondary btn-sm' href='#' data-toggle='modal' data-target='#exampleModal' data-title='Title text' data-body='Text body'>Launch modal</a>
我在 Bootstrap 文档中找到了这个关于如何使用 JQuery 改变模态内容的示例:
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">Open modal for @mdo</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@fat">Open modal for @fat</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@getbootstrap">Open modal for @getbootstrap</button>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">New message</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Recipient:</label>
<input type="text" class="form-control" id="recipient-name">
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Message:</label>
<textarea class="form-control" id="message-text"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Send message</button>
</div>
</div>
</div>
</div>
<script>
$('#exampleModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // Button that triggered the modal
var title = button.data('title') // Extract info from data-* attributes
var recipient = button.data('id') // Extract info from data-* attributes
var modal = $(this)
modal.find('.modal-title').text(title)
modal.find('.modal-body input').val(recipient)
})
</script>
问题是它似乎只更改表单控件(输入或文本区域)的内容。我的目标是更改短信,但我没能做到。有什么想法吗?
谢谢!
我终于找到了解决方案。有人告诉任何地方模态可以被视为任何 html 标签,所以我决定测试使用 Vanilla JS 访问 DOM 像这样:
<script>
$('#exampleModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget)
var title = button.data('title')
var body = button.data('body')
var modal = $(this)
modal.find('.modal-title').text(title)
document.getElementById('body').innerHTML = body
})
</script>
在模态框内,我用这种方式创建了一个 'body' div:
<div class="modal-body">
<div id="body"></div>
</div>
最后打开模态和传递数据的按钮的代码是:
<a class='btn btn-secondary btn-sm' href='#' data-toggle='modal' data-target='#exampleModal' data-title='Title text' data-body='Text body'>Launch modal</a>