我正在尝试让我的引导模式使用ajax检索数据:
<a href="{{ path('ajax_get_messages', { 'superCategoryID': 35, 'sex': sex }) }}" data-toggle="modal" data-target="#birthday-modal">
<img src="{{ asset('bundles/yopyourownpoet/images/messageCategories/BirthdaysAnniversaries.png') }}" alt="Birthdays" height="120" width="109"/>
</a>
我的模态:
<div id="birthday-modal" class="modal hide fade">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3>Birthdays and Anniversaries</h3>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<a href="#" data-dismiss="modal" class="btn">Close</a>
{#<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>#}
</div>
</div>
当我点击链接时,会显示模态,但正文为空。此外,我没有看到任何ajax请求。 我正在使用Bootstrap 2.1.1
我做错了什么?
答案 0 :(得分:40)
最高投票回答是deprecated in Bootstrap 3.3 and will be removed in v4。试试这个:
JavaScript的:
// Fill modal with content from link href
$("#myModal").on("show.bs.modal", function(e) {
var link = $(e.relatedTarget);
$(this).find(".modal-body").load(link.attr("href"));
});
Html(基于the official example。请注意,对于Bootstrap 3. *我们设置data-remote="false"
以禁用不推荐使用的Bootstrap加载函数):
<!-- Link trigger modal -->
<a href="remoteContent.html" data-remote="false" data-toggle="modal" data-target="#myModal" class="btn btn-default">
Launch Modal
</a>
<!-- Default bootstrap modal example -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
答案 1 :(得分:27)
在Bootstrap 3中轻松完成,如下所示:
<a data-toggle="modal" href="remote.html" data-target="#modal">Click me</a>
答案 2 :(得分:21)
以下是我解决问题的方法,可能对某些人有用:
使用boostrap 2.1.1
似乎无法使用Ajax模式所以我最终自己编写了代码:
$('[data-toggle="modal"]').click(function(e) {
e.preventDefault();
var url = $(this).attr('href');
//var modal_id = $(this).attr('data-target');
$.get(url, function(data) {
$(data).modal();
});
});
调用模态的链接示例:
<a href="{{ path('ajax_get_messages', { 'superCategoryID': 6, 'sex': sex }) }}" data-toggle="modal">
<img src="{{ asset('bundles/yopyourownpoet/images/messageCategories/BirthdaysAnniversaries.png') }}" alt="Birthdays" height="120" width="109"/>
</a>
我现在通过ajax发送整个模态标记。
的信用