1
Updated code. Edson Alves' help.
I’m having trouble calculating Total sum when unchecking the checkbox. At the moment I am checking, it calculates correctly, the problem is in the unchecking that should subtract.
$(".c").click(function() {
var values = $(this).val().split('|');
if ($(this).prop('checked')) {
var quantity = $("table[id^=tabelaIE]").length;
$(this).data('qty', quantity);
var table = $("#tabelaIE").clone(true)
.attr('id', function() {
return this.id + quantity;
})
.find(':text,:file')
.attr('id', function() {
return this.id + quantity;
})
.val("")
.end();
//Adiciona valores 1|Taxa de locação|+|15.00|FIX|%|POR|VLT
table.find('.tNome').text(values[1]);
table.find('.tDias').text($("#dias").val());
table.find('.tValor').text(values[3]);
tTotal = values[3] * $("#dias").val();
table.find('.tTotal').text(tTotal.toFixed(2));
values.forEach(function(valor, index) {
table.find('[class="split' + (index + 1) + '"]').val(valor)
});
table.appendTo('#abc');
var oldVal = $('#somaTabelaIE').val();
$('#somaTabelaIE').val(eval(oldVal || 0) + eval(tTotal))
} else {
var oldVal = $('#somaTabelaIE').val();
$('#somaTabelaIE').val(oldVal - eval(tTotal))
//remove a table que pertence ao checkbox
$("table#tabelaIE" + $(this).data('qty')).remove()
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" id="dias" value="2">
<div id="abc">
<div id="abc">
<table id="tabelaIE" width='400' border='0'>
<tr>
<td class="tNome" width='164'></td>
<td class="tDias" width='36' align='right'></td>
<td class="tValor" width='53' align='right'></td>
<td class="tTotal" width='119' align='right'></td>
<td class="tValores">
<input type="hidden" class="split1" value="">
<input type="hidden" class="split2" value="">
<input type="hidden" class="split3" value="">
<input type="hidden" class="split4" value="">
<input type="hidden" class="split5" value="">
<input type="hidden" class="split6" value="">
<input type="hidden" class="split7" value="">
<input type="hidden" class="split8" value="">
</td>
</tr>
</table>
<br>
Soma Total: <input type="text" name="somaTabelaIE" id="somaTabelaIE" value="">
</div>
</div>
<fieldset class="scheduler-border">
<legend class="scheduler-border">Impostos/Encargos</legend>
<div id="D1">1.
<input name="opcoes[]" class="c obrigatorio" type="checkbox" value="1|Texto 1|+|15.00|FIX|%|POR|VLT" alt="+ 0.15 FIX %" title="Texto 1"> <b> Texto 1 </b>
<input type="text" value="" class="i">
</div>
<div id="D2">2.
<input name="opcoes[]" class="c " type="checkbox" value="2|Texto 2|+|5.00|DIA||MON|DIA" alt="+ 5.00 DIA " title="Texto 2"> Texto 2
<input type="text" value="" class="i">
</div>
<div id="D3">3.
<input name="opcoes[]" class="c " type="checkbox" value="3|Texto 3|+|30.00|FIX||MON|VLT" alt="+ 30.00 FIX " title="Texto 3"> Texto 3
<input type="text" value="" class="i">
</div>
<div id="D4">4.
<input name="opcoes[]" class="c " type="checkbox" value="3|Texto 4|+|35,00|FIX||MON|VLT" alt="+ 35,00 FIX " title="Texto 4"> Texto 4
<input type="text" value="" class="i">
</div>
</fieldset>
Your question is not very clear, but if you just want to clone you can use the jQuery clone:
$('.checkbox').clone(true)
. The true indicates that you want to clone along with your values– edson alves
@edsonalves edited my code, I am not able to delete the table created by unchecking the checkbox. Know how to do?
– Tiago
you can use the function
prop
to determine if the checkbox is checked by clicking:$(this).prop('checked')
and decide whether to remove or add– edson alves
Your new problem is that when you enter the
else
the valuetTotal
does not exist. You can copy the linetTotal = values[3] * $("#dias").val();
into theelse
or throw it to before theif
– edson alves