变量范围:在回调函数中看不到
基本上,我有几个我想要的朋友:
- 在地图上查看
- 在表格中列出
我在朋友列表上有一个循环,对于每次迭代我:
- 创建谷歌地图标记
- 在列表中添加朋友
第一个问题:对于标记,我添加了一个事件侦听器以便能够显示信息窗口。我使用标记的标题来检索朋友的信息...这只是一个愚蠢的解决方法,因为在检索朋友的 ajax 方法中不知道“id”。这是我的第一个问题。我希望能够使用 title 作为真正的标题(而不是 id),并设法以另一种方式在 ajax 回调中获取 id。
第二个问题 对于列表,我为每个朋友添加一个“li”条目,并在 li.a 链接上添加一个事件侦听器,以便能够显示朋友的详细信息。也没有看到“id”。
这两个问题属于同一类。
下面是我完成的代码。显然出了问题,但无法弄清楚我需要更改什么才能在回调函数中访问 id 。
注意:我使用 jquery mobile 作为 HTML5 应用程序。
for(j=0;j<friends.length;j++){
var lat = friends[j][1];
var long = friends[j][2];
var id = friends[j][3];
var latLng = new google.maps.LatLng(lat,long);
var marker = new google.maps.Marker({ position: latLng,
map: map,
title: id
});
// Add friends to list
$("#friend_list ul").append('<li><a id="details' + id + '" rel="external" data-transition="slide">' + id + '</a></li>');
// Add event handler when details:id is clicked
$('#details' + id).click(function(){
alert(id + ' clicked'); // DOES NOT WORK AS ID IS NOT KNOWN IN THIS METHOD
});
// Add listener on click action for marker created above
// I PASS THE TITLE OF SELF AS ID IS NOT KNOWN IN THIS METHOD
// IF I use: url: "/friend/" + id => ONLY THE LAST VALUE OF THE ID IS TAKEN INTO ACCOUNT
google.maps.event.addListener(marker, 'click', function() {
self = this;
$.ajax({
url: "/friend/" + self.title,
success: function(details){
var message = details["lastname"] + ' ' + details["firstname"];
var infowindow = new google.maps.InfoWindow(
{ content: message,
size: new google.maps.Size(50,50)
}
);
infowindow.open(map,self);
}
});
});
}
知道可能出了什么问题吗?
Basically, I have several friends that I want to:
- see on a map
- list in a table
I have a loop on the friends list, for each iteration I:
- create a google maps marker
- add friend in a list
FIRST PROBLEM: For the marker I add an event listener to be able to display an infowindow. I use the title of the marker to be able to retrieve info on the friend... that's only a silly wokaround because "id" is not known within the ajax method retrieving the friend. This is my first problem. I'd like to be able to use title for a real title (not an id) and manage to get the id within the ajax callback in another way.
SECOND PROBLEM For the list, I add a "li" entry for each friend and add an event listener on the li.a link to be able to display the friend details. "id" is not seen neither.
Those 2 problems are of the same familly.
Below is the code I've done. Obviously something wrong but cannot figure out what I need to change to access id within the callback functions.
note: I use jquery mobile for a HTML5 application.
for(j=0;j<friends.length;j++){
var lat = friends[j][1];
var long = friends[j][2];
var id = friends[j][3];
var latLng = new google.maps.LatLng(lat,long);
var marker = new google.maps.Marker({ position: latLng,
map: map,
title: id
});
// Add friends to list
$("#friend_list ul").append('<li><a id="details' + id + '" rel="external" data-transition="slide">' + id + '</a></li>');
// Add event handler when details:id is clicked
$('#details' + id).click(function(){
alert(id + ' clicked'); // DOES NOT WORK AS ID IS NOT KNOWN IN THIS METHOD
});
// Add listener on click action for marker created above
// I PASS THE TITLE OF SELF AS ID IS NOT KNOWN IN THIS METHOD
// IF I use: url: "/friend/" + id => ONLY THE LAST VALUE OF THE ID IS TAKEN INTO ACCOUNT
google.maps.event.addListener(marker, 'click', function() {
self = this;
$.ajax({
url: "/friend/" + self.title,
success: function(details){
var message = details["lastname"] + ' ' + details["firstname"];
var infowindow = new google.maps.InfoWindow(
{ content: message,
size: new google.maps.Size(50,50)
}
);
infowindow.open(map,self);
}
});
});
}
Any idea of what could be wrong ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我没有阅读你的整个问题,因为我需要快点,但
文档对象在范围内始终可见,
如果你使用,
你将能够
从任何地方
获取,这并不是说将所有内容都放在文档对象中是一个很好的设计决策,但是让你知道;)
I did not read you whole problem because I need to hurry up but
the document object is always visible in scope
if you use
you will be able to get the
from anywhere
not that it is a good design decision to have everything in the document object but to let you know ;)
标记不是谷歌地图标记的变量吗?
所以你应该能够访问marker.title来获取id。
Is marker not the variable for the google maps marker?
So you should be able to access marker.title to get the id.