Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jquery使用动态id计算和_Javascript_Jquery_Google Maps - Fatal编程技术网

Javascript Jquery使用动态id计算和

Javascript Jquery使用动态id计算和,javascript,jquery,google-maps,Javascript,Jquery,Google Maps,我有一个谷歌地图,当用户添加一个标记时,它会计算该标记和上一个标记之间的距离。我通过jQuery将距离添加到表中。距离很好。我想有一个“累计距离”列,它将距离添加到该点。所以它看起来像这样: $("#distance_0").val(); 我正在为每行上的跨距动态分配ID。我不确定如何创建累积距离列。这是我到目前为止所做的: var currentIndex=0; var distancenm = Math.round((((google.maps.geometry.spherical.

我有一个谷歌地图,当用户添加一个标记时,它会计算该标记和上一个标记之间的距离。我通过jQuery将距离添加到表中。距离很好。我想有一个“累计距离”列,它将距离添加到该点。所以它看起来像这样:

$("#distance_0").val();

我正在为每行上的跨距动态分配ID。我不确定如何创建累积距离列。这是我到目前为止所做的:

 var currentIndex=0;
 var distancenm = Math.round((((google.maps.geometry.spherical.computeDistanceBetween(FromLocation, ToLocation))/1000)) * 0.539957);
 var accdistance = $("#distance_0").val();
 var accdistance2 = $("#distance_1").val();
 var accdistancetotal = (accdistance+accdistance2);

 $('#table').append('<tr id="table_row_'+(currentIndex-1)+'">'+
    '<td><span id="distance_'+(currentIndex-1)+'" name="distance_[]" class="distance">'+distancenm+' </span></td>'+
    '<td><span id="accdistance_'+(currentIndex-1)+'" name="accdistance_[]" class="accumulateddistance">'+accdistancetotal+' </span></td>'+
    '</td>'
因为我不知道用户将添加多少点,所以我需要这样做:每行的累积距离列正好是当前行的距离列+上一行的距离列之和

编辑:在马里奥的建议之后,看起来是这样的:


要计算当前所有距离的总和,您需要执行以下操作:

var accDistance = 0;
$('.distance').each(function() {
  accDistance += parseInt($(this).text());
});
accDistance += distancenm;

我认为它会起作用,因为在您的示例中,您使用的是
val()
标记中,它是不正确的。

哦,我明白了。我尝试了你的代码,但它工作正常,但我对显示为0的第一个累积距离行有问题。它基本上跳过了一个。我将编辑我的问题,并发布一个看起来像什么的图像。请检查我的编辑,你需要将累积的总距离与你的距离相加nm variablE