0

好的,所以能够从 PHP 页面获取 JSON,但是当用户单击li内部时,ul#stores它不会运行。这就像

$("ul#stores").html("");
myMarkers = {"markers": data}

没有更新,它无法在列表中找到它,并且下面的功能无法运行。完整的代码在最后。我不确定这是否是解释我的问题的最佳方式。

现场演示@http://brightchoice.com.au/map.html

$("ul#stores li").on("click", function(event){
            var store = $(this).text();

            $("ul#stores").hide();
            $("#storeinfo").show();
            $.each(myMarkers.markers, function(i, data){

            if(store == data.name) {
                $("#storeinfo").append("<a href='map.html'><div class='greenbar'><div class='body'><div>  <span></span> </div> <p>Back</p></div></a></div><h1>"+data.name+"</h1>"+data.phone+"<br><br>"+data.address);

                //console.log(data.address);
                //console.log(data.state); // and more
             }
            });

            //searchlistings($(this).text());

        });

完整代码

<script type="text/javascript">
// <![CDATA[
 $(document).ready(function(){
        //set up markers
         var myMarkers = {"markers": [{"id": 1,"name": "Belconnen","url_name": "belconnen","address": "Shop 176, 3rd Floor, Westfield Shopping Centre, Benjamin Way","suburb": "Belconnen","state": "act","postcode": 2618,"country": "Australia","phone": "(02) 6251 1838","fax": "","photo": "dcddfcb5c806b2255c611bd2d108cead.JPG","text": "","display": 1,"lat": -35.238428606,"lng": 149.065917134,"email": "tc.belconnen@telechoice.com.au","dealer_code": 63014,"region_id": 3,"type": 4}]};

//set up map options
        defaultmap();

        $("a#backtomap").on("click", function(event){
        defaultmap();
            $("ul#stores").show();
            $("#storeinfo").hide();
        });


        $("button#search").on("click", function(event){

            var postcode = $("input#postcode").val();
            $.getJSON("http://vipcashback.com/system/classes/core.php?task=postcode&mycode="+postcode, function(data) {
            $("ul#stores").html("");
              listings = {"markers": data}
                $("#map").mapmarker({
                    zoom : 10,
                    center : postcode+" australia",
                    markers : listings
                });

                $.each(myMarkers.markers, function(i, data){

                });
            });
        });

        $("ul#stores li").on("click", function(event){
            var store = $(this).text();
            alert(store);
            $("ul#stores").hide();
            $("#storeinfo").show();
            $.each(myMarkers.markers, function(i, data){

            if(store == data.name) {
                $("#storeinfo").append("<a href='map.html'><div class='greenbar'><div class='body'><div>  <span></span> </div> <p>Back</p></div></a></div><h1>"+data.name+"</h1>"+data.phone+"<br><br>"+data.address);

                //console.log(data.address);
                //console.log(data.state); // and more
             }
            });

            //searchlistings($(this).text());

        });


        function defaultmap(){
            $("#map").mapmarker({
                zoom : 3,
                center : 'Australia',
                markers : myMarkers
            });
        }

});

// ]]>
</script>
4

2 回答 2

1

如果您要动态创建元素,则应使用:

$('ul#stores li).live('click',function(){ ...});

live 是为了让未来的元素可以自动绑定到事件。

于 2012-05-30T00:15:45.733 回答
0

通过插件代码,它需要 PHP 页面的 JSON 中缺少的经度和纬度

插件代码 -

             if(latitude!="" && longitude!=""){
                var marker = new google.maps.Marker({
                    map: map, 
                    position: new google.maps.LatLng(latitude,longitude),
                    animation: google.maps.Animation.DROP,
                    icon: icon
                });

                // Set up markers with info windows 
                google.maps.event.addListener(marker, 'click', function() {
                    // Close all open infowindows
                    if (infowindow) {
                        infowindow.close();
                    }

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

                    infowindow.open(map,marker);
                });
            }
        });
于 2012-05-30T00:47:22.303 回答