Error in jquery div display

Asked

Viewed 78 times

0

Good morning, stack overflow

I have the following problem.

I try to display a content on the screen, part of this content gets duplicated.

arrayObjCat9 = [];
$(".form-group").find('*').each(function() {
  var id = $(this).attr("id");
  //testo qual categoria
  if ($("#" + id).data("categoria") == 9) {
    i = 0; //limita o número de execuções, pois estava imprimindo muitas vezes
    if (i == 0) {
      if ($(this).is(':checked')) { //testo se existe campo chegado
        //texto se quem está checado é quem estou querendo
        if ($("input[type='checkbox'][name='Suporteeconsultoria']").is(':checked')) {
          //insiro dentro do array

          arrayObjCat9.push({
            part_number: $('#' + id).data('part-number'),
            nome_item: $('#' + id).data('nome-item'),
            qtde: 1,
            preco_unitario: vlrUnitario,
            preco_total: precoTotal
          });
        }
      }
      i++;
    }
    //testo se o array está vazio
    if (arrayObjCat9.length != 0) {
      //percorro todo o array e crio os dados
      $.each(arrayObjCat9, function(index, value) {
        $(".itensSuporteConsultoria").append("<tr><td>" + value.part_number + "</td> <td> " + value.nome_item + " </td><td> " + value.qtde + " </td><td> " + value.preco_unitario.toFixed(2).replace(".", ",") + "</td><td>" + value.preco_total.toFixed(2).replace(".", ",") + " </td></tr>");
        totalGarantia += value.preco_total; //somo o resultado final
      });
      //exibo o total na tela
      $(".totalSuporteConsultoria").text('Total: $' + parseFloat(totalGarantia, 10).toFixed(2).replace(/[.]/g, ",").replace(/\d(?=(?:\d{3})+(?:\D|$))/g, "$&.").toString());
      $(".suporteConsultoria").fadeIn();
    }
  }
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>



<!-- HTML COM OS CHECKBOX -->

<div class="col-md-12 col-sm-12 suporteConsultoria">
  <div class="portlet grey-cascade box">
    <div class="portlet-title">
      <div class="caption">
        <i class="fa fa-cogs"></i>Suporte e Consultoria</div>
      <div class="actions"></div>
    </div>
    <div class="portlet-body">
      <div class="table-responsive">
        <table class="table table-hover table-bordered table-striped">
          <thead>
            <tr>
              <th> Part Number </th>
              <th> Descrição </th>
              <th> Quantidade </th>
              <th> Preço Unitário </th>
              <th> Preço Total </th>
            </tr>
          </thead>
          <tbody class="itensSuporteConsultoria"></tbody>
        </table>
        <div class="pull-right totalSuporteConsultoria"></div>
      </div>
    </div>
  </div>
</div>

As the result is displayed, it was only to display that LSA01 once. Ignore Undefined

inserir a descrição da imagem aqui

Someone give me a light?

When I select both checkboxes, this returns to me:

inserir a descrição da imagem aqui

  • Is this javascript code complete? I tried playing in Jsfiddle but couldn’t, the id variable is missing. When this if is called?

  • All right, I put the missing one

  • Why set zero to each cycle of .each and check if it is 0 at the same time? i = 0; if( i == 0 ){, does this i = 0; shouldn’t be outside the .each?

  • Yeah, I’m a little nervous and aimless with this mistake

  • I changed the code, it still shows the same result, only when I select the 2 checkbox, when I select only the first one, it is "right"

  • It is difficult to put all the code here, but it gives a look pls

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.