getElementById另一个问题

时间:2011-01-11 18:21:59

标签: javascript html

作为Felix Kling如此出色地回答的扩展问题,我现在需要对部署的数据表进行两部分检查。

任何人都可以告诉我如何添加到下面的代码,以便我不仅可以将pcode字段的值复制到数组中,还可以检查复选框中是否有相应行号的检查,即route2是检查然后添加到数组但是如果未检查route3则将其排除。

         function GetRoute() 
     { 
        var from = document.getElementById('inpAddr').value; 
        var locations = [from]; 
        for(var i = 2; i <= 400; i++) { 
            var element = document.getElementById('pcode' + i);
            if(element === null) { break; } 
            locations.push(element.innerHTML); 
        } 
        var options = new VERouteOptions(); 
        options.DrawRoute = true;
        options.RouteColor = new VEColor(63,160,222,1);
        options.RouteWeight = 3;
        options.RouteCallback = onGotRoute;  
        options.SetBestMapView = true;
        options.DistanceUnit   = VERouteDistanceUnit.Mile;
        options.ShowDisambiguation = true;          
        map.GetDirections(locations,options); 
     } 

提前致谢!

贾斯汀

2 个答案:

答案 0 :(得分:1)

for( var i = 2 ; (element = document.getElementById('pcode' + i)) && i <= 400; i++ )
{ 
  if( document.getElementById('route' + i).checked )
  {
      locations.push( element.innerHTML ); 
  }
}

答案 1 :(得分:0)

function GetRoute() { 
  var from = document.getElementById('inpAddr').value; 
  var locations = [from]; 

  // My Modification Starts Here....

  var maxLoopValue = 400;
  var pcode, currentRoute, nextRoute;

  for(var i = 2; i <= maxLoopValue; i++) { 
    pcode = document.getElementById('pcode' + i);
    currentRoute = document.getElementById('route' + i);

    // Make sure the currentRoute is 'checked'
    if (currentRoute.checked) {

      // Make sure there is a 'next' route before trying to check it
      if (i < maxLoopValue) {
        nextRoute = document.getElementById('route' + (i+1));

        // Make sure the 'next' route is 'checked'
        if (nextRoute.checked) {
          locations.push(element.innerHTML); 
        }
      } else {
        // This is the last route, since we know it's checked, include it
        locations.push(element.innerHTML); 
      }
    }
  } 

  // My Modification Ends Here....

  var options = new VERouteOptions(); 
  options.DrawRoute = true;
  options.RouteColor = new VEColor(63,160,222,1);
  options.RouteWeight = 3;
  options.RouteCallback = onGotRoute;  
  options.SetBestMapView = true;
  options.DistanceUnit   = VERouteDistanceUnit.Mile;
  options.ShowDisambiguation = true;          
  map.GetDirections(locations,options); 
}