My function in JavaScript/JQuery doesn’t return anything; I want it to return the result for me but it won’t. As you can see, the return is the $("#resultado").html("O Valor do produto ficara "+resultado); but this doesn’t appear as it should when the function is run:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript">
$("#target").submit(function(){
var preco_dolar = $("#preco_dolar").val();
var dolar = $("#dolar").val();
var frete = $("#frete").val();
if ($('#drop').is(':checked'))
{
var drop = 5.8;
}else var drop = 0;
if(typeof preco_dolar == "undefined"){
alert("Erro ! Falta preco do produto");
}
if(typeof frete == "undefined"){
var frete = 40;
}
if(typeof dolar == "undefined"){
var dolar = 2.16;
}
var resultado = (preco_dolar*dolar)+frete;
$("#resultado").html("O Valor do produto ficara "+resultado);
});
</script>
Can someone point out where I’m going wrong please?
First you need to wrap your script into
Second for your improvement, don’t use
Use instead
That is the same as you would do:
To make 0 a valid value, use
Also don’t use
var dolar = 0;twice. If you declared it already, simply usedolar =, to not overwrite the already declared var.