2016-10-23 35 views
-1

我正在使用Google地圖自動完成功能。我在單個形式的多個地址選擇器,其在所述波紋管圖像中示出:如何從多個Google地圖自動完成獲取輸入ID

enter image description here

我已經添加了兩個額外的自定義地址與被波紋管圖像中示出的谷歌地圖自動完成的結果:

enter image description hereenter image description here

添加額外的自定義地址的代碼波紋管式給出:

if (AType == "Home") 
          icss = "pac-icon icon-home"; 
         else if (AType == "Office") 
          icss = "pac-icon icon-office"; 
         else 
          icss = "pac-icon icon-other"; 
$(".pac-container").append('<div class="pac-item cs" onmousedown="ShowMap(e);"><span class="' + icss + '"></span><span class="pac-item-query"><span class="pac-matched"></span>' + Add + '</span> <span></span></div>'); 

與Pickup和Droffof位置一起添加相同的辦公室和家庭地址。現在我需要知道點擊/選定地址(自定義地址)選擇器的ID。我使用了JavaScript方法(onmousedown =「ShowMap(e);」)來獲取ID。但我無法得到預期的結果。誰能幫我?

function ShowMap(e) { 
     e = e || window.event;   
     var elementId = e.target ? e.target.id : e.srcElement.id; 
     alert(elementId); 
    } 

回答

1

您正在使用的錯誤e變量。
該變量定義在其他地方(在全球範圍內),否則你會得到一個錯誤:

This example shows the error if you don't have the e variable defined:

function ShowMap(e) { 
 
    console.log(e) 
 
}
<div id="areasearch" class="pac-item cs" onmousedown="ShowMap(e)">a</div>

如果你想 - 你可以通過當前元素你點擊使用this,這樣就很容易得到該元素的id

var e; 
 

 
function ShowMap(el, e) { 
 
    console.log(el.getAttribute('id')) 
 
}
<div id="areasearch1" class="pac-item cs" onmousedown="ShowMap(this, e)">a</div> 
 
<div id="areasearch2" class="pac-item cs" onmousedown="ShowMap(this, e)">b</div>

+0

感謝您的回答 –