2011-12-18 170 views
2
var EventAddress=$(".EventAddress").text(); 
$(".EventDirectionLink a").attr("href", url + EventAddress); 

這是我用來從表格單元格中獲取地址值並將它作爲查詢字符串傳遞給地圖url的jquery,但它不工作?我究竟做錯了什麼??將查詢字符串添加到URL

<div class="driving-directions-link"> 
<a href="http://maps.google.com/maps?q=">Get Direction</a> 
</div> 

<table cellpadding="10" class ="EventDetail"> 
    <tr> 
     <td class="TableFields">Who Should Enroll?:</td> 
     <td>Everyone 18 and older who would like to attend</td> 
    </tr> 
    <tr> 
     <td class="TableFields">Location:</td> 
     <td class="EventAddress">1300 Albany Street,Beech Grove ,IN</td> 
    </tr> 
</table> 

回答

0
<div class="EventDirectionsLink"> 
<a href="http://maps.google.com/maps?q=">Get Direction</a> 
</div> 

<table cellpadding="10" class ="EventDetail"> 
    <tr> 
     <td class="TableFields">Who Should Enroll?:</td> 
     <td>Everyone 18 and older who would like to attend</td> 
    </tr> 
    <tr> 
     <td class="TableFields">Location:</td> 
     <td class="EventAddress">1300 Albany Street,Beech Grove ,IN</td> 
    </tr> 
</table> 
var EventAddress=$(".EventAddress").text(); 
$(".EventDirectionsLink > a").attr("href", $('.EventDirectionsLink > a).attr('href') + EventAddress); 
0
$(".driving-directions-link > a").attr("href", "http://maps.google.com/maps?q=" + EventAddress); 
4

試試這個:

$('td.EventAddress').click(
    function(){ 
     $(".driving-directions-link a").attr("href", $(this).attr('href') + encodeURIComponent($(this).text)); 
    }); 

JS Fiddle demo

簡化上述一點,以避免使用.attr()方法兩次(第二時間不必要地):

$('td.EventAddress').click(function(){ 
    $(".driving-directions-link a").attr("href", function(i, h) { 
     return h + encodeURIComponent($(this).text()); 
    }); 

JS Fiddle demo

參考文獻:

0
var EventAddress = encodeURIComponent($(".EventAddress").text()); 
$(".EventDirectionLink a").attr("href", url + "q=" + EventAddress); 

記住,你使用它之前的查詢字符串解碼。

0

我有一個類似的問題,嘗試添加使用查詢字符串:

$(this).attr("href", url + queryString);

它被添加URL而不是查詢字符串。

我使用的JavaScript CONCAT方法來代替,這似乎工作:

var queryString = '?myVar=test'; 
//Replace the url with an additional query string 
$(this).attr('href', $(this).attr('href').concat(queryString)); 
相關問題