2011-12-18 9 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を。 (不必要に二回目)を2回.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);

それはのqueryString URLを追加することではなくました。

私の代わりにjavascriptのconcatメソッドを使用し、これは動作するように見えた:

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