带有ASP经典循环的Javascript不重复

时间:2014-04-23 12:12:36

标签: javascript google-maps asp-classic

我在ASP循环中使用以下javascript有一个小问题

<script type="text/javascript">

var geocoder, location1, location2, gDir;

function initialize() {
    geocoder = new GClientGeocoder();
    gDir = new GDirections();
    GEvent.addListener(gDir, "load", function() {
        var drivingDistanceMiles = gDir.getDistance().meters / 1609.344;
        var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
    var drivingTime = gDir.getDuration().html
        document.getElementById("<%=brokeridnum%>").innerHTML = '<strong>Address 1: </strong>' + location1.address + ' <br /><strong>Address 2: </strong>' + location2.address + '<br /><strong>Driving Distance: </strong>' + drivingDistanceMiles.toFixed(2) + ' miles taking ' + drivingTime;
    });
}
</script>
<script type="text/javascript">



    function showLocation(startpc, endpc) {
        geocoder.getLocations(startpc, function (response) {
            if (!response || response.Status.code != 200)
            {
                alert("Sorry, we were unable to geocode the first address");
            }
            else
            {
                location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
                geocoder.getLocations(endpc, function (response) {
                    if (!response || response.Status.code != 200)
                    {
                        alert("Sorry, we were unable to geocode the second address");
                    }
                    else
                    {
                        location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
                        gDir.load('from: ' + location1.address + ' to: ' + location2.address);
                    }
                });
            }
        });

    }

    </script>

当循环运行时,javascript只在元素内显示一次。

&lt;%= brokeridnum%&gt;在JS中插入一个数字,这正在查看chrome中的源代码。

id为brokeridnum的元素也在工作。

<script type="text/javascript">

   initialize();
   showLocation("postcode1","<%=destpc%>");
   </script>

<p id="<%=rsbkr("broker_id")%>"></p>

这就是函数的调用方式

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

Without seeing the entire script I can only guess...
Maybe instead of looping the entire script, try 
only looping the function call with a   parameter like this:

<% Do   . . . %>
  <script type="text/javascript">
     initialize(<%= brokeridnum %>);
  </script>
<% LOOP . . . %>

The function now outside the loop waits for the brokerid parameter...

function initialize(brokerid){
.....
.....
document.getElementById(brokerid).innerHTML = '......
}

Sorry about abbreviating the code a bit, but I hope I helped.