0

如何让模型名称显示在模态中。换句话说,我如何正确地将变量从 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>
4

2 回答 2

2

使用可以定位的占位符元素 - 替换{{modelname}}<span id="modelnameplaceholder"></span>. 然后,您可以执行以下操作:

$('#modelnameplaceholder').replaceWith(modelname);

参考http ://api.jquery.com/replaceWith/

示例 jsFiddle:http: //jsfiddle.net/G82mk/

于 2013-03-15T04:10:45.583 回答
0

在这一行之后:

var modelname = $('#modelname').text();

添加这个:

$('#myModal .modal-body').html($('#myModal .modal-body').html().replace('{{modelname}}', modelname));
于 2013-03-15T03:44:16.257 回答