Infowindow Button jQuery Event

时间:2016-12-08 12:25:22

标签: javascript jquery google-maps button click

我无法执行当有人点击infoWindow div上的编辑,共享或删除时调用的jQuery函数。

var markers = [];
for(i=0; i<array.length; ++i) {
  var marker = new google.maps.Marker({
    position: {lat: parseFloat(array[i]['latitude']), lng: parseFloat(array[i]['longitude'])},
    map: map
  });

  var id = array[i]['id'];
  var edit = 'edit', share = 'share', del = 'delete';
  var cString = '<div style="margin: auto; text-align: center; font-family: Tahoma, Geneva, sans-serif;"><strong>Location Name: </strong>' + array[i]['title']
  + '<br><strong>Location Description: </strong>' + array[i]['description']
  + '<br><br><br><div class="btn-group"><button type="button" class="btn btn-primary '+edit+'" id="' + id + '">Edit</button><button type="button" class="btn btn-primary '+share+'" id="' + id + '">Share</button><button type="button" class="btn btn-primary '+del+'" id="' + id + '">Delete</button></div>';

  contentString.push(cString);
  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
      infoWindow.setContent(contentString[i]);
      infoWindow.open(map, marker);
    }
  })(marker, i));

  // this is the function
  $('button').click(function() {
    console.log('clicked');
  });

  markers.push(marker);
}

对于分配给infoWindow的按钮,它不会显示已点击的内容,但会显示其他按钮,例如注销,查看个人资料等。

Array是一个具有以下结构的JSON数组:

[
  {
    id:"1"
    description:"I am Loving It! ↵McArabia Combo Meal: 520 Rs/-"
    latitude:"25.28919345"
    longitude:"67.11113134"
    title:"McDonalds"
    type:"favourite"
  },//....
  //......
]

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

您在页面加载后动态添加这些按钮。您需要使用.on()函数在按钮上附加click事件。

$(document).on( "click", "button", function() {
  console.log('clicked');
});

并且不要在for循环中添加此事件绑定。把它放在文件准备好了。 这仅用于基本信息,请按this link了解有关on()的更多信息以及如何使用正确的选择器/容器。

答案 1 :(得分:3)

进一步@anu评论:

这是因为infoWindow仅在函数infoWindow.open(map, marker);中添加到DOM,所以当您将click绑定到按钮时,infoWindow的按钮不包括在内

和实例:

$(document).on('click', '.info-button', function(){
  alert('button clicked');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Info windows</title>
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>

      // This example displays a marker at the center of Australia.
      // When the user clicks the marker, an info window opens.

      function initMap() {
        var uluru = {lat: -25.363, lng: 131.044};
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 4,
          center: uluru
        });

        var contentString = '<div id="content">'+
            '<button class="info-button">Click on it</button>' +
            '</div>';

        var infowindow = new google.maps.InfoWindow({
          content: contentString
        });

        var marker = new google.maps.Marker({
          position: uluru,
          map: map,
          title: 'Uluru (Ayers Rock)'
        });
        marker.addListener('click', function() {
          infowindow.open(map, marker);
        });
      }
    </script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?&callback=initMap">
    </script>
  </body>
</html>