2013-03-31 95 views
0

我想傳遞一個url用於我的模態,該模態依賴於從我的主HTML文檔獲取adder變量。我知道如何將變量傳遞給用作文本的模態(使用跨度),但不知道如何在鏈接<a href>標記中使用變量。傳遞一個變量在模態中用作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傳遞的URL。

我該怎麼做?

回答

0

這應該工作

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

似乎沒有工作... – user1328021

+0

對不起......這將是$( 「#A」)。HREF Element.attribute =值 – Shreyas