The total value appears

3

I am making a system for a virtual store, from which the user will have the value of the package. For example: R $ 12.000,00, however he will have the option to choose the 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

How to select Yes, the travel insurance value be summed with the value of the package and change the final value? Ex.:

(without insurance) Value: R $ 12,000.00

When you click Yes, change to:

(with insurance) Insurance: R $ 500.00 Value: R $ 12,500.00

I tried using the code below but it is 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!

    
asked by anonymous 18.08.2015 / 15:25

1 answer

4

I saw that you already solved the problem. But I'll leave the answer as an 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>SeguroViagem?<br><inputid="comSeguro" type='radio' name='Seguro' value='Sim'> Sim
<input type='radio' name='Seguro' value='Não'> Não

<p id="total">Valor total:</p>
    
18.08.2015 / 15:46