Show total value

Asked

Viewed 98 times

3

I am making a system for a virtual store, of which the user will have the value of the package. For example: R$ 12.000,00, however he will have the option to choose travel insurance through the code below:

Seguro Viagem?<br>
<input type='radio' name='Seguro' value='Sim'> Sim
<input type='radio' name='Seguro' value='Não'> Não

Is it possible to select Yes, the value of the trip insurance to be added to the value of the package and change the final value? Ex.:

( uninsured ) Value: R$ 12,000,00

When you click Yes, change to:

( with insurance ) Insurance: R$ 500,00 Value: R$ 12,500,00

I tried to use the code below, but it’s not working:

<script language="Javascript">
function soma(){
valorSeguro = 500.00;
valorPacote = 1200.00;
e = valorSeguro + valorPacote; 
if(e.toFixed(2) == "NaN"){
document.getElementById("total").innerHTML = "USD 0.00";
}else{
document.getElementById("total").innerHTML = "USD "+e.toFixed(2)+"";
}
}
</script>
Seguro Viagem?<br>
<input type='radio' name='Seguro' onchange="soma()" value='Sim'> Sim
<input type='radio' name='Seguro' value='Não'> Não<br><br>

<div id="total" style="font-family:Arial; font-size:16px">USD 12.000,00</div>

Thank you!

  • 1

    Let’s delete the comments?

1 answer

4


I saw that you already solved the problem. But I will leave the answer as alternative.

var valorSeguro = 500.00;
var valorPacote = 1200.00;

$('input:radio[name="Seguro"]').change( function() {
  if ($(this).is(':checked') && $(this).val() == 'Sim') {
    var valorTotal = valorSeguro + valorPacote;
    $("#total").html("Valor total: USD " + valorTotal);
  } else {
    $("#total").html("Valor total: USD " + valorPacote);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Seguro Viagem?<br>
<input id="comSeguro" type='radio' name='Seguro' value='Sim'> Sim
<input type='radio' name='Seguro' value='Não'> Não

<p id="total">Valor total:</p>

Browser other questions tagged

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