传递变量以用作模态中的href属性

时间:2013-03-31 14:01:17

标签: javascript jquery html twitter-bootstrap

我想传递一个url,用于我的modal,它依赖于从我的主HTML文档中获取adder变量。我知道如何将变量传递给模式以用作文本(使用span),但不知道如何在链接<a href>标记中使用变量。

我在每个文档中加粗了重要的区域。 Javascript从主HTML文档中获取adder变量并将其存储为URL。

主要HTML文档:

<a href = "#myModal" class = "lib" data-toggle="modal" style = "text-align:center; color: #FF761A;" href="/models/{{symbol.id}}/view_model/"><span id = "modelname"> {{symbol.modelname.upper}}</span> - <span id = "format">{{ symbol.format }}</span>**<span id = "adder" style="visibility:hidden;"> {{symbol.adder}}</span>**</a>

JAVASCRIPT:

 <script type="text/javascript">
    $(document).on("click", ".lib", function () {
         var modelname = $('#modelname').text();
         $('#modelnameplaceholder').replaceWith(modelname);     
         var format = $('#format').text();
         $('#formatplaceholder').replaceWith(format); 
         var adder = $('#adder').text();
         $('#adderplaceholder').replaceWith(adder);   
         **var URL = "/profiles/profile/" + adder + "/"**
         $('#urlplaceholder').replaceWith(URL);   
         var type = $(this).closest("ul").attr('id');
         $('#typeplaceholder').replaceWith(type); 
         $('#myModal').modal('show');
    });
   </script>

模式对话:

<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"><span id="modelnameplaceholder"></span>&nbsp;<span id="typeplaceholder"></span> for <span id="formatplaceholder"></span></h3>
</div>
<div class="modal-body">
<div style="background-image: url({{ STATIC_URL }}pinax/images/placeholder.png); background-repeat:no-repeat;background-position:center center; height: 300px; width: 100%; "></div>
<br>
<div id = "download-button" style="text-align-center">
<a href="{{MEDIA_URL}}{{item.content}}" class="btn btn-success btn-large" type="submit"><i class="icon-download icon-white"></i>&nbsp;Download {{item.type}}</a>
</div>
<br>
<br>
<br>
<div style="position:0px;float:right;">
**Added by <a href = urlplaceholder><span id="adderplaceholder"></span></a>**

我想将Added by <username>链接到从javascript传递的网址。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

这应该有效

var url = "http://google.com/";
$("#a").href = url;