从模态(noob)中的javascript访问变量

时间:2013-03-15 03:38:04

标签: javascript jquery

如何让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>

2 个答案:

答案 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));