0

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?

4 Answers 4

1

First you need to wrap your script into

// here $("#target") is nothing

$(document).ready(function () {
    // here $("target") exists
});

Second for your improvement, don't use

if(typeof dolar == "undefined"){
    dolar = 2.16;
}

Use instead

if(!dolar){
    dolar = 2.16;
}

That is the same as you would do:

if(!dolar && dolar == 0 && dolar == "" && dolar == false && dolar == undefined){
    dolar = 2.16;
}

To make 0 a valid value, use

if (!dolar && dolar !== 0) {
    dolar = 2.16;
}

Also don't use var dolar = 0; twice. If you declared it already, simply use dolar =, to not overwrite the already declared var.

Sign up to request clarification or add additional context in comments.

4 Comments

1. You only have to wrap the script if it appears before the form is rendered on the page. 2. What if dolar is zero? You just reset it! typeof checks are not bad, would probably be better as just if (dolar === undefined) {
@epascarello yeah you're right because of 0 being a valid value. he actually declares jquery script tag right before his script, so it's predictable the element doesn't exist already.
Probably even easier to just do var dolar = $("#dolar").val() || 2.16; As long as val() returns a string. :)
@epascarello hah expanding your tricks: var dolar = +$("#dolar").val() || 2.16 makes sure you have a number. but we are losing now the valid 0 so an if check will be required.
1

You are not calling the form submission so the page is refreshing and resetting the page. Use preventDefault to stop the form submission.

$("#target").submit(function (evt) {
    evt.preventDefault();
    ...

Comments

1
return false;

should suffice.

alternatively, you could prevent the default behavior by

e.preventDefault();
e.stopPropagation();

Comments

0

You don't have a return statement. Javascript functions require a return statement to return a value:

function test() {
    return 5;
}

1 Comment

The question sounds like this is what the OP was referring too, but that is not the case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.