Sum of values with javascript, error?

2

I have a solution that does the sum of values with respect to the value of a input check only if it is selected, but if you select the items from first to last, the sum is normally done but only the last input of the table the value is zeroed, sample code follows.

function selecionaDactes() {
  var ids = document.getElementsByClassName('editar');
  gravaArray(ids);
}

function gravaArray(dados) {
  var array_dados = dados;
  var teste = Array();
  var valor = Array();
  for (var x = 0; x <= array_dados.length; x++) {
    if (typeof array_dados[x] == 'object') {
      if (array_dados[x].checked) {
        teste[x] = array_dados[x].id;
        valor[x] = parseFloat(array_dados[x].value);
      }
    }
  }
  teste = teste.filter(Boolean);
  document.getElementById('docs').value = teste;
  valorTotal(valor);
}

function valorTotal(valor) {
  var array_valores = valor;
  var total = 0;
  for (var a = 0; a <= Object.keys(array_valores).length; a++) {
    if (array_valores[a] != '' && array_valores[a] != undefined && array_valores[a] != NaN) {
      total = parseFloat(total + array_valores[a]);
    }
  }
  total = total.toLocaleString('pt-br', {
    minimumFractionDigits: 2,
    currency: 'BRL'
  });
  document.getElementById('valor').value = total;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><!--LatestcompiledandminifiedCSS--><linkrel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<form name="teste" method="#" action="">
  <label>Documents:
  <input type="text" name="docs" id="docs"/></label>
  <br>
  <label>Values:
  <input type="text" name="valor" id="valor"/></label>
  <br>
</form>
<table id="tbl_dactes" border="0" class="table table-condensed table-hover">
  <thead>
    <tr>
      <th align="center">#</th>
      <th>Transportador</th>
      <th>Conhecimento</th>
      <th>R$ Frete</th>
      <th>Data Emissão</th>
      <th>Fornecedor</th>
      <th><span class="glyphicon glyphicon-pencil"></span></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="438283" value="65.58"> </td>
      <td>Dll logistica</td>
      <td>438283</td>
      <td><span class="valor_dacte">R$ 65,58</span></td>
      <td>03/05/2017</td>
      <td>Aliexpress</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="3171" value="73.62"> </td>
      <td>Inter Modal</td>
      <td>3171</td>
      <td><span class="valor_dacte">R$ 73,62</span></td>
      <td>05/05/2017</td>
      <td>Ebay</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="191513" value="65"> </td>
      <td>Italian Group Global</td>
      <td>191513</td>
      <td><span class="valor_dacte">R$ 65,00</span></td>
      <td>09/05/2017</td>
      <td>FastBay</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="18977" value="157.74"> </td>
      <td>All In Global</td>
      <td>18977</td>
      <td><span class="valor_dacte">R$ 157,74</span></td>
      <td>08/05/2017</td>
      <td>Tesla</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="18969" value="181.32"> </td>
      <td>Inter Modal</td>
      <td>18969</td>
      <td><span class="valor_dacte">R$ 181,32</span></td>
      <td>08/05/2017</td>
      <td>Adidas</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="190999" value="65"> </td>
      <td>Global Latin</td>
      <td>190999</td>
      <td><span class="valor_dacte">R$ 65,00</span></td>
      <td>04/05/2017</td>
      <td>Footage</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="1296" value="253.34"> </td>
      <td>E-Log Transportes Rodoviarios LTDA</td>
      <td>1296</td>
      <td><span class="valor_dacte">R$ 253,34</span></td>
      <td>09/05/2017</td>
      <td>Nike</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="431587" value="191.25"> </td>
      <td>Vast</td>
      <td>431587</td>
      <td><span class="valor_dacte">R$ 191,25</span></td>
      <td>30/03/2017</td>
      <td>Etti</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="5161" value="400.92"> </td>
      <td>Tico Tico</td>
      <td>5161</td>
      <td><span class="valor_dacte">R$ 400,92</span></td>
      <td>29/04/2017</td>
      <td>LaCoste</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="18216" value="148.8"> </td>
      <td>Italian Log</td>
      <td>18216</td>
      <td><span class="valor_dacte">R$ 148,80</span></td>
      <td>21/04/2017</td>
      <td>Vine</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="435335" value="377.15"> </td>
      <td>Porto Ferreira Portugal xpress</td>
      <td>435335</td>
      <td><span class="valor_dacte">R$ 377,15</span></td>
      <td>18/04/2017</td>
      <td>Sardinha</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
  </tbody>
</table>

If I select only the last 2 records the value becomes 0.00, I am not finding the error, I believe the error is when generating the array with the values.

    
asked by anonymous 17.05.2017 / 15:33

2 answers

3

If I understand the problem correctly, it happens that when you fill in the array value, you use

valor[x] = parseFloat(array_dados[x].value);

Note that this can result in a fill-in without an order, as it will only add in array when condition if(array_dados[x].checked) is satisfied.

I simply added an auxiliary counter contAux to set the index in array and the algorithm worked as expected.

function selecionaDactes() {
  var ids = document.getElementsByClassName('editar');

  gravaArray(ids);
}

function gravaArray(dados) {
  var array_dados = dados;
  var contAux = 0;
  var teste = Array();
  var valor = Array();

  for (var x = 0; x <= array_dados.length; x++) {
    if (typeof array_dados[x] == 'object') {
      if (array_dados[x].checked) {
        teste[x] = array_dados[x].id;
        //valor[x] = parseFloat(array_dados[x].value);
        valor[contAux] = parseFloat(array_dados[x].value);
        contAux++;
      }
    }
  }
  teste = teste.filter(Boolean);
  document.getElementById('docs').value = teste;
  valorTotal(valor);
}

function valorTotal(valor) {
  var array_valores = valor;
  var total = 0;

  for (var a = 0; a <= Object.keys(array_valores).length; a++) {
    if (array_valores[a] != '' && array_valores[a] != undefined && array_valores[a] != NaN) {

      total = parseFloat(total + array_valores[a]);
    }
  }

  total = total.toLocaleString('pt-br', {
    minimumFractionDigits: 2,
    currency: 'BRL'
  });
  //alert(total);

  document.getElementById('valor').value = total;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><!--LatestcompiledandminifiedCSS--><linkrel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<form name="teste" method="#" action="">
  <label>Documents:
  <input type="text" name="docs" id="docs"/></label>
  <br>
  <label>Values:
  <input type="text" name="valor" id="valor"/></label>
  <br>
</form>
<table id="tbl_dactes" border="0" class="table table-condensed table-hover">
  <thead>
    <tr>
      <th align="center">#</th>
      <th>Transportador</th>
      <th>Conhecimento</th>
      <th>R$ Frete</th>
      <th>Data Emissão</th>
      <th>Fornecedor</th>
      <th><span class="glyphicon glyphicon-pencil"></span></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="438283" value="65.58"> </td>
      <td>Dll logistica</td>
      <td>438283</td>
      <td><span class="valor_dacte">R$ 65,58</span></td>
      <td>03/05/2017</td>
      <td>Aliexpress</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="3171" value="73.62"> </td>
      <td>Inter Modal</td>
      <td>3171</td>
      <td><span class="valor_dacte">R$ 73,62</span></td>
      <td>05/05/2017</td>
      <td>Ebay</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="191513" value="65"> </td>
      <td>Italian Group Global</td>
      <td>191513</td>
      <td><span class="valor_dacte">R$ 65,00</span></td>
      <td>09/05/2017</td>
      <td>FastBay</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="18977" value="157.74"> </td>
      <td>All In Global</td>
      <td>18977</td>
      <td><span class="valor_dacte">R$ 157,74</span></td>
      <td>08/05/2017</td>
      <td>Tesla</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="18969" value="181.32"> </td>
      <td>Inter Modal</td>
      <td>18969</td>
      <td><span class="valor_dacte">R$ 181,32</span></td>
      <td>08/05/2017</td>
      <td>Adidas</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="190999" value="65"> </td>
      <td>Global Latin</td>
      <td>190999</td>
      <td><span class="valor_dacte">R$ 65,00</span></td>
      <td>04/05/2017</td>
      <td>Footage</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="1296" value="253.34"> </td>
      <td>E-Log Transportes Rodoviarios LTDA</td>
      <td>1296</td>
      <td><span class="valor_dacte">R$ 253,34</span></td>
      <td>09/05/2017</td>
      <td>Nike</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="431587" value="191.25"> </td>
      <td>Vast</td>
      <td>431587</td>
      <td><span class="valor_dacte">R$ 191,25</span></td>
      <td>30/03/2017</td>
      <td>Etti</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="5161" value="400.92"> </td>
      <td>Tico Tico</td>
      <td>5161</td>
      <td><span class="valor_dacte">R$ 400,92</span></td>
      <td>29/04/2017</td>
      <td>LaCoste</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="18216" value="148.8"> </td>
      <td>Italian Log</td>
      <td>18216</td>
      <td><span class="valor_dacte">R$ 148,80</span></td>
      <td>21/04/2017</td>
      <td>Vine</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="435335" value="377.15"> </td>
      <td>Porto Ferreira Portugal xpress</td>
      <td>435335</td>
      <td><span class="valor_dacte">R$ 377,15</span></td>
      <td>18/04/2017</td>
      <td>Sardinha</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
  </tbody>
</table>

On this other snippet, I indicated on the console the error that was happening with your code, for example, mark the last checkbox and click to do the calculation

function selecionaDactes() {
  var ids = document.getElementsByClassName('editar');

  gravaArray(ids);
}

function gravaArray(dados) {
  var array_dados = dados;
  var contAux = 0;
  var teste = Array();
  var valor = Array();

  for (var x = 0; x <= array_dados.length; x++) {
    if (typeof array_dados[x] == 'object') {
      if (array_dados[x].checked) {
        teste[x] = array_dados[x].id;
        valor[x] = parseFloat(array_dados[x].value);
        //valor[contAux] = parseFloat(array_dados[x].value);
        contAux++;
      }
    }
  }
  teste = teste.filter(Boolean);
  document.getElementById('docs').value = teste;
  valorTotal(valor);
}

function valorTotal(valor) {
  var array_valores = valor;
  var total = 0;
  console.log(valor);
  for (var a = 0; a <= Object.keys(array_valores).length; a++) {
    if (array_valores[a] != '' && array_valores[a] != undefined && array_valores[a] != NaN) {

      total = parseFloat(total + array_valores[a]);
    }
  }

  total = total.toLocaleString('pt-br', {
    minimumFractionDigits: 2,
    currency: 'BRL'
  });
  //alert(total);

  document.getElementById('valor').value = total;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><!--LatestcompiledandminifiedCSS--><linkrel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<form name="teste" method="#" action="">
  <label>Documents:
  <input type="text" name="docs" id="docs"/></label>
  <br>
  <label>Values:
  <input type="text" name="valor" id="valor"/></label>
  <br>
</form>
<table id="tbl_dactes" border="0" class="table table-condensed table-hover">
  <thead>
    <tr>
      <th align="center">#</th>
      <th>Transportador</th>
      <th>Conhecimento</th>
      <th>R$ Frete</th>
      <th>Data Emissão</th>
      <th>Fornecedor</th>
      <th><span class="glyphicon glyphicon-pencil"></span></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="438283" value="65.58"> </td>
      <td>Dll logistica</td>
      <td>438283</td>
      <td><span class="valor_dacte">R$ 65,58</span></td>
      <td>03/05/2017</td>
      <td>Aliexpress</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="3171" value="73.62"> </td>
      <td>Inter Modal</td>
      <td>3171</td>
      <td><span class="valor_dacte">R$ 73,62</span></td>
      <td>05/05/2017</td>
      <td>Ebay</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="191513" value="65"> </td>
      <td>Italian Group Global</td>
      <td>191513</td>
      <td><span class="valor_dacte">R$ 65,00</span></td>
      <td>09/05/2017</td>
      <td>FastBay</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="18977" value="157.74"> </td>
      <td>All In Global</td>
      <td>18977</td>
      <td><span class="valor_dacte">R$ 157,74</span></td>
      <td>08/05/2017</td>
      <td>Tesla</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="18969" value="181.32"> </td>
      <td>Inter Modal</td>
      <td>18969</td>
      <td><span class="valor_dacte">R$ 181,32</span></td>
      <td>08/05/2017</td>
      <td>Adidas</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="190999" value="65"> </td>
      <td>Global Latin</td>
      <td>190999</td>
      <td><span class="valor_dacte">R$ 65,00</span></td>
      <td>04/05/2017</td>
      <td>Footage</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="1296" value="253.34"> </td>
      <td>E-Log Transportes Rodoviarios LTDA</td>
      <td>1296</td>
      <td><span class="valor_dacte">R$ 253,34</span></td>
      <td>09/05/2017</td>
      <td>Nike</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="431587" value="191.25"> </td>
      <td>Vast</td>
      <td>431587</td>
      <td><span class="valor_dacte">R$ 191,25</span></td>
      <td>30/03/2017</td>
      <td>Etti</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="5161" value="400.92"> </td>
      <td>Tico Tico</td>
      <td>5161</td>
      <td><span class="valor_dacte">R$ 400,92</span></td>
      <td>29/04/2017</td>
      <td>LaCoste</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="18216" value="148.8"> </td>
      <td>Italian Log</td>
      <td>18216</td>
      <td><span class="valor_dacte">R$ 148,80</span></td>
      <td>21/04/2017</td>
      <td>Vine</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="435335" value="377.15"> </td>
      <td>Porto Ferreira Portugal xpress</td>
      <td>435335</td>
      <td><span class="valor_dacte">R$ 377,15</span></td>
      <td>18/04/2017</td>
      <td>Sardinha</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
  </tbody>
</table>
    
17.05.2017 / 15:47
2

I've changed your code to be simpler, mainly because you can make a selector with :checked so you do not need the loops.

function selecionaDactes() {
  var selecionados = [].slice.call(document.querySelectorAll('#tbl_dactes :checked'));

  var ids = selecionados.map(function(el) {
    return el.id;
  }).join(', ');
  document.getElementById('docs').value = ids;


  var total = selecionados.reduce(function(soma, el) {
    return soma + Number(el.value);
  }, 0);
  document.getElementById('valor').value = total.toLocaleString('pt-br', {
    minimumFractionDigits: 2,
    currency: 'BRL'
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><!--LatestcompiledandminifiedCSS--><linkrel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<form name="teste" method="#" action="">
  <label>Documents:
  <input type="text" name="docs" id="docs"/></label>
  <br>
  <label>Values:
  <input type="text" name="valor" id="valor"/></label>
  <br>
</form>
<table id="tbl_dactes" border="0" class="table table-condensed table-hover">
  <thead>
    <tr>
      <th align="center">#</th>
      <th>Transportador</th>
      <th>Conhecimento</th>
      <th>R$ Frete</th>
      <th>Data Emissão</th>
      <th>Fornecedor</th>
      <th><span class="glyphicon glyphicon-pencil"></span></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="438283" value="65.58"> </td>
      <td>Dll logistica</td>
      <td>438283</td>
      <td><span class="valor_dacte">R$ 65,58</span></td>
      <td>03/05/2017</td>
      <td>Aliexpress</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="3171" value="73.62"> </td>
      <td>Inter Modal</td>
      <td>3171</td>
      <td><span class="valor_dacte">R$ 73,62</span></td>
      <td>05/05/2017</td>
      <td>Ebay</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="191513" value="65"> </td>
      <td>Italian Group Global</td>
      <td>191513</td>
      <td><span class="valor_dacte">R$ 65,00</span></td>
      <td>09/05/2017</td>
      <td>FastBay</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="18977" value="157.74"> </td>
      <td>All In Global</td>
      <td>18977</td>
      <td><span class="valor_dacte">R$ 157,74</span></td>
      <td>08/05/2017</td>
      <td>Tesla</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="18969" value="181.32"> </td>
      <td>Inter Modal</td>
      <td>18969</td>
      <td><span class="valor_dacte">R$ 181,32</span></td>
      <td>08/05/2017</td>
      <td>Adidas</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="190999" value="65"> </td>
      <td>Global Latin</td>
      <td>190999</td>
      <td><span class="valor_dacte">R$ 65,00</span></td>
      <td>04/05/2017</td>
      <td>Footage</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="1296" value="253.34"> </td>
      <td>E-Log Transportes Rodoviarios LTDA</td>
      <td>1296</td>
      <td><span class="valor_dacte">R$ 253,34</span></td>
      <td>09/05/2017</td>
      <td>Nike</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="431587" value="191.25"> </td>
      <td>Vast</td>
      <td>431587</td>
      <td><span class="valor_dacte">R$ 191,25</span></td>
      <td>30/03/2017</td>
      <td>Etti</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="5161" value="400.92"> </td>
      <td>Tico Tico</td>
      <td>5161</td>
      <td><span class="valor_dacte">R$ 400,92</span></td>
      <td>29/04/2017</td>
      <td>LaCoste</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="18216" value="148.8"> </td>
      <td>Italian Log</td>
      <td>18216</td>
      <td><span class="valor_dacte">R$ 148,80</span></td>
      <td>21/04/2017</td>
      <td>Vine</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
    <tr>
      <td><input class="editar" type="checkbox" name="checkboxes" id="435335" value="377.15"> </td>
      <td>Porto Ferreira Portugal xpress</td>
      <td>435335</td>
      <td><span class="valor_dacte">R$ 377,15</span></td>
      <td>18/04/2017</td>
      <td>Sardinha</td>
      <td><a href="#" data-toggle="modal" data-target="#dan" onclick="selecionaDactes()"><span class="glyphicon glyphicon-plus"></span></a></td>
      <!-- inicio modal -->

    </tr>
  </tbody>
</table>
    
17.05.2017 / 15:58