ReferenceError:未定义crimeID

时间:2012-08-30 03:02:17

标签: javascript ajax google-maps-api-3

这部分代码下载用作地图上数据源的xml文件。 数据放在一个信息窗口中,该窗口有一个调用函数confirmData(data)的按钮,该函数加载一个php文件。但是,当我单击按钮时,错误显示“RefereceError:crimeID未定义”。如何调试?

     var url = "crimeFetch3.php"; 
    downloadUrl(url, function(data) {   
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("crime");
        for (var i = 0; i < markers.length; i++) {
            var point = new google.maps.LatLng(
                parseFloat(markers[i].getAttribute("lat")),
                parseFloat(markers[i].getAttribute("lng")));
            var crimeID = markers[i].getAttribute("crimeID");   
            var id = markers[i].getAttribute("id"); 
            var type = markers[i].getAttribute("type");
            var address = markers[i].getAttribute("address");
            var date = markers[i].getAttribute("date");
            var date2 = date +  " (yyyy-mm-dd)";
            var desc = markers[i].getAttribute("desc");
            var status = markers[i].getAttribute("status");
            if(status == "confirmed"){
                var html ="<table>"+

                    "<tr><td><b>Crime: </b> </td> <td><b><u> "  + type + "</u></b></td></tr></br>" +
                    "<tr><td><b>Address: </b> </td> <td> " + address + "</td></tr></br>" +
                    "<tr><td><b>Date: </b></td> <td> " + date2 + "</td></tr></br" +
                    "<tr><td><b>Description: /b></td> <td> " + desc + " </a></td></tr></br>" +
                    "<tr><td><b>Status: </b></td> <td> " + status + "</td></tr></table>";

            }else{
                var html ="<table>"+

                    "<tr><td><b>Crime: </b></td> <td><b><u> "  + type + "</u></b></td></tr></br>" +
                    "<tr><td><b>Address: </b></b></td> <td> " + address + "</td></tr></br>" +
                    "<tr><td><b>Date: </b></td> <td> " + date2 + "</td></tr></br" +
                    "<tr><td><b>Description: </b></td> <td> " + desc + " </a></td></tr></br>" +
                    "<tr><td><b>Status: </b></td> <td> " + status + "</a></td></tr></br>" +
                    "<tr><td></td><td><input type='submit' value='Confirm' onclick='confirmData(crimeID)'/></td></tr></table>";
            }
            var icon = markerIcons[type] || {};
            var marker = new google.maps.Marker({
                            map: map,
                            position: point,
                            shadow: shadow,
                            icon: markerIcons[id],
                            title: type
            });
            bindInfoWindow(marker, map, infoWindow, html);


        }
    }); 

} 
    function bindInfoWindow(marker, map, infoWindow, html) {
        google.maps.event.addListener(marker, 'click', function() {
            infoWindow.setContent(html);
            infoWindow.open(map, marker);
        });
    }

    function downloadUrl(url, callback) {
        var request = window.ActiveXObject ?
        new ActiveXObject('Microsoft.XMLHTTP') :
        new XMLHttpRequest;

        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                request.onreadystatechange = doNothing;
                callback(request, request.status);
            }
        };

        request.open('GET', url, true);
        request.send(null);
    }

    function doNothing() {};

    var xmlhttp;
    function ajaxFunction(){
        if(window.ActiveXObject){
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else if(window.XMLHttpRequest){
            xmlhttp = new XMLHttpRequest();
        }
        else{
            alert("Get another Web Browser!");
            }

    }
    function confirmData(data) {
        var crimeNum = data;
        ajaxFunction();


        function stateChanged(){
            if(xmlhttp.readyState == 4 && xmlhttp.status==200){
                alert(xmlhttp.responseText);
            }
        }
            var url = "confirmCrime.php?crimeNum=" + crimeNum;  
                xmlhttp.onreadystatechange=stateChanged();
                xmlhttp.open("GET",url,true);
                xmlhttp.send(null);
    }

1 个答案:

答案 0 :(得分:1)

crimeID不是全局变量。

执行此代码后:

downloadUrl(url, function(data) {
   ...
   var crimeID = ...
});

任何东西都不能再引用变量crimeID了。 这部分onclick='confirmData(crimeID)实际上超出了该代码块的范围。

更改此部分:

"<tr><td></td><td><input type='submit' value='Confirm' onclick='confirmData(crimeID)'/></td></tr></table>";

到此:

"<tr><td></td><td><input type='submit' value='Confirm' onclick='confirmData(" + crimeID + ")'/></td></tr></table>";