-3
I am mounting an additional calculator for a product, but the additional ones are calculated in percentage. For example:
If the product is R$ 50,00 and is marked the checkbox that adds 40%, should change the price div to R$ 70,00.
If the first two are marked checkbox (40% and 15%), the expected value is R$ 127,50. The percentage should be based on the original price (in the case of two or more additional marked).
<input class="form-check-input" type="checkbox" value="40" id="adicional1">
<label class="form-check-label" for="defaultCheck1">
Adicional 1 - <strong>+40%</strong>
</label>
<input class="form-check-input" type="checkbox" value="15" id="adicional2">
<label class="form-check-label" for="defaultCheck1">
Adicional 2 - <strong>+15%</strong>
</label>
<input class="form-check-input" type="checkbox" value="30" id="adicional3">
<label class="form-check-label" for="defaultCheck1">
Adicional 3 - <strong>+30%</strong>
</label>
<div id="price">R$ 50,00</div>
The code I tried to adapt and it didn’t work because it summing up the values and not the percentage.
$(document).ready(function(){
$('.form-check-input').click(function (){
var total = 0;
$('.form-check-input:ckecked').each(function(){
total += parseInt($(this).val());
var formatado = total.toLocaleString("pt-BR", { style: "currency" , currency:"BRL"});
});
$('#price').html(formatado);
});
});
How to transform this code so that some percentage?
First of all, do not use greetings or greetings, see what kind of behavior is expected from users? and how to create a [Mre] to prepare a question GOOD and CLEAR
– gleisin-dev
to make these "additional" it is necessary to make the percentage calculation first (to get the correct result, if it will not just add up the values) the interpreter does not understand that % refers to percentage, so the calculation
valor * (porcentagem / 100)
that in this case50 * (40 / 100) = 20
– gleisin-dev