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
Someone give me a light?
When I select both checkboxes, this returns to me:
Is this javascript code complete? I tried playing in Jsfiddle but couldn’t, the id variable is missing. When this if is called?
– Renato Diniz
All right, I put the missing one
– gabrielfalieri
Why set zero to each cycle of
.each
and check if it is0
at the same time?i = 0; if( i == 0 ){
, does thisi = 0;
shouldn’t be outside the.each
?– Guilherme Nascimento
Yeah, I’m a little nervous and aimless with this mistake
– gabrielfalieri
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"
– gabrielfalieri
It is difficult to put all the code here, but it gives a look pls
– gabrielfalieri