2011-01-11 54 views
0

作爲Felix Kling如此出色的回答,我現在需要對部署的數據表進行兩部分檢查。getElementById另一個問題

任何人都可以告訴我如何添加到下面的代碼,以便我不僅可以將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); 
    } 

在此先感謝!

賈斯汀

回答

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

感謝它的偉大工程的明星! –

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