0

我有一堆标记index page,我在其中循环创建和注册听众。我单击其中一个标记,它会将我带到next page需要anchor button知道哪个标记启动操作的位置。我逐步有以下情况:

  • 单击标记 1//outputs a correct id 1 in console
  • 带我到下一页 //outputs a correct id 1 in console on clicking anchor
  • 返回索引页面并单击标记 2//outputs a correct id 2 in console
  • 带我到下一页//outputs both ids 1 and 2 in console on clicking anchor

最后一步是问题出在我想要的地方id 2。事实上,如果我第三次重复这个过程,我会得到所有的 id 1、2 和 3,而id 3在这种情况下我只想要 id。

我的代码:

$.each(otherLocations, function(index, value){
  var markerOtherLocations = new MarkerWithLabel({
    position: new google.maps.LatLng(value.latitude, value.longitude),
    map: map,
    title: value.name+" "+value.distance,
    icon: iconImage,
    labelContent: value.name+" "+value.distance,
    labelAnchor: new google.maps.Point(50, 0),
    labelClass: "labels", // the CSS class for the label
    labelStyle: {opacity: 0.60}
  });


  google.maps.event.addListener(markerOtherLocations, 'click', function() {
    $.mobile.changePage("#detail-page", { transition: "flip"} );
    console.log(value.localurl);//Outputs correct url

    $("#ref").on("click", function(){  //The problem is in this anchor click
      console.log(value.localurl);//Outputs the current as well as all the previous urls
    });
  });
});
4

1 回答 1

1

每次单击标记OtherLocations 时,它都会注册一个导致问题的全新onclick事件回调。#ref记住一个事件可以被许多回调注册。考虑下面的代码:

$("#ref").on("click", function(){  
  console.log('do function A');//register A first
});

$("#ref").on("click", function(){ 
  console.log('do function B');//register B later, which won't be overridden.
});

//If you click #ref then, it'll output A and B, following the registered sequence before.

所以在我看来,你的代码可能是:

google.maps.event.addListener(markerOtherLocations, 'click', function() {
  $.mobile.changePage("#detail-page", { transition: "flip"} );
  console.log(value.localurl);//Outputs correct url
  $("#ref").data('origin',value.localurl);
});

$("#ref").on("click", function(){ // register once 
  console.log($(this).data('origin'));
});
于 2013-07-19T22:33:54.760 回答