0

index page에 마커가 있으며 루프에서 청취자를 등록합니다. 그 마커 중 하나를 클릭하면 next page으로 이동합니다. anchor button은 마커가 작업을 시작했는지 알아야합니다. 나는 현명한 다음과 같은 시나리오 단계를 겪고 :Google지도 v3의 루프에있는 마커 수신기

  • 클릭 마커 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 3 만 필요한 반면 1, 2, 3의 모든 ID를 얻습니다.

내 코드 : 당신이 markerOtherLocations을 클릭하면

$.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 
    }); 
    }); 
}); 

답변

1

때마다,이 문제를 일으키는 #ref에 대한 새로운 onclick 이벤트 콜백을 등록합니다. 이벤트는 많은 콜백에 의해 등록 될 수 있음을 기억하십시오. 코드 아래 고려 :

$("#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')); 
}); 
+0

예, 당신이 일을 제안한 방법. 이제 클릭 한 최신 마커의'handle'을 받았습니다. 그러나'each 루프 '안에 있었기 때문에 나는 여러 번 같은'handle'을 얻고있었습니다. 루프 외부에 놓았으니 지금은 완벽합니다. 그러나 다시 캐치는,'새로 고침 할 때 전체 스크립트가 다시 실행되는 결과로 '인덱스 페이지'에 새로 고침 기능이 있기 때문입니다. 이것 때문에 나는 올바른'handle'을 여러 번 다시 얻는다. 하지만 그건 중요하지 않습니다. 적어도 나는 정확한 것을 얻고있다. 많은 감사 ... ...-) – SASM