如何让modelname显示在模态中。换句话说,如何将变量从javascript正确传递给我的模态?
我有以下javascript:
<script type="text/javascript">
$(document).on("click", ".cadlib", function () {
var modelname = $('#modelname').text();
$('#myModal').modal('show');
});
一个模态对话框:
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">My Modal</h3>
</div>
<div class="modal-body">
Here's all the info you would want to know about {{modelname}}
</div>
</div>
答案 0 :(得分:2)
使用您可以定位的占位符元素 - 将{{modelname}}
替换为<span id="modelnameplaceholder"></span>
之类的内容。然后,您可以执行以下操作:
$('#modelnameplaceholder').replaceWith(modelname);
参考:http://api.jquery.com/replaceWith/
示例jsFiddle :http://jsfiddle.net/G82mk/
答案 1 :(得分:0)
这一行之后:
var modelname = $('#modelname').text();
添加:
$('#myModal .modal-body').html($('#myModal .modal-body').html().replace('{{modelname}}', modelname));