javascript - 将数据传递给模态( Bootstrap )

标签 javascript google-maps twitter-bootstrap

我在谷歌地图上有多个标记,当我单击它们时,我需要打开一个模式窗口。问题是每个标记都有一个特定的 id,我需要在每个模式中使用该 id。

模态 html 是:

<div class="modal fade" id="imageModal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">Modal title</h4>
      </div>
      <div class="modal-body">
      </div>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
  </div>
</div>

调用该函数的 JavaScript 代码是:

function createMarker(pos, t, map) {
    var marker = new google.maps.Marker({
        position: pos,
        map: map,  // google.maps.Map
        index: t
    });
    google.maps.event.addListener(marker, 'click', function() {
      $('#imageModal').modal();
      //alert("I am marker " + marker.index);
    });
    return marker;
 }

所以在imageModal中我需要访问动态的marker.index信息。我怎样才能做到这一点?有什么想法吗?

最佳答案

首先,我不认为 indexMarkerOption在 Google map v3 中。

我是如何得到这个的,我在 MarkerOption 中使用了 title 选项,并将其设置为动态字符串,然后在 addListener 事件中访问它。

具体操作方法如下:

function createMarker(pos, t, map) {
    var marker = new google.maps.Marker({
        position: pos,
        map: map,  // google.maps.Map
        title: t   //where t must be a string
    });
    google.maps.event.addListener(marker, 'click', function() {
      $('#imageModal').modal();
      alert("I am marker " + this.getTitle() );  
    });
 }

希望这对你有帮助!!

关于javascript - 将数据传递给模态( Bootstrap ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18694541/

相关文章:

javascript - AngularJS:使用列表项作为单独列表的变量

javascript - 这是从带有对象的数组创建列表的正确方法吗?

google-maps - 谷歌地图 API v3 : Does placemarker exist on given LatLng?

javascript - Google map Javascript API - 如何删除与成人相关的搜索结果

java - 如何在fragment_map.xml 中启用位置并启用工具栏?

html - 如何防止 Bootstrap 列相互重叠

css - 搜索下拉建议

html - 如何使用 twitter bootstrap 右对齐元素?

javascript - SublimeVideo JavaScript

javascript - 检测 View 中的动画元素