2017-07-22 14 views
-1

イベントで呼び出されるJavaScript関数があります。これは次のようになります。JavaScriptにデータをプッシュするonClickイベントで呼び出される関数

function showArrays(event) { 
    // Taken from Google Maps API Documentation 
    // Since this polygon has only one path, we can call getPath() to return the 
    // MVCArray of LatLngs. 
    var vertices = this.getPath(); 
    var contentString = 'A'; 

    // Replace the info window's content and position. 
    infoWindow.setContent(contentString); 
    infoWindow.setPosition(event.latLng); 

    infoWindow.open(map); 
} 

私はこのファイルを同じファイル内の以前のJavaScript関数から呼び出します。私はそれがこのコードを使用して呼び出します。

function initMap(){ 
    var myString = 'abc'; 
    region.addListener('click', showArrays); 

    infoWindow = new google.maps.InfoWindow; 
} 

私は「contentString」はその値に設定することができるようにもSHOWARRAYSに「のmyString」を渡したいです。しかし、私はどのように関数間でデータをプッシュできるのか分かりません。イベントパラメータとデータパラメータの両方を取るようにshowArraysを編集しようとしましたが、これはうまくいきません。私の構文が間違っているからです。どのように私はこれを行うことができる任意のアイデア?おそらくJQueryで?

答えて

2
function showArrays(event,_str) { 
    // Taken from Google Maps API Documentation 
    // Since this polygon has only one path, we can call getPath() to return the 
    // MVCArray of LatLngs. 
    var vertices = this.getPath(); 
    var contentString = 'A'; 

    // Replace the info window's content and position. 
    infoWindow.setContent(contentString+_str); 
    infoWindow.setPosition(event.latLng); 

    infoWindow.open(map); 
} 

function initMap(){ 
    var myString = 'abc'; 
    region.addListener('click', function(event){ 
    showArrays(event, 'myString') 
    }); 

    infoWindow = new google.maps.InfoWindow; 
} 

はそのようなのmyStringはあなたの応答に収まらないオブジェクト

region.addListener('click', function(event){ 
    showArrays(event, 'myString') 
    }); 
+0

でSHOWARRAYSを発射しますコールバック関数とコールバック関数を呼び出すのですか? – taurus

+1

私はチェックをしています –

+1

私はちょうどコールバック関数に引数を渡すことができる方法を説明しています –

関連する問題