Undefined variable

Asked

Viewed 138 times

2

Hello,

I’m having a problem recovering the global variable totalin a script. In an Alert inserted in the same scope it is displayed normal, but in a declaration it is not returned, it appears Undefined. follows the excerpt:

preco = "";
qtd = "";
total = "";

function keypressed(obj, e, f) {
var tecla = (window.event) ? e.keyCode : e.which;
preco = f;
qtd = tecla - 48;
total = qtd * preco;

if (tecla == 8 || tecla == 0 || tecla == 13)
    return true;
if (tecla < 48 || tecla > 57)
    return false;
}

$("#confirma").confirm({
title: "Confirmação",
ERRO >>> text: "O valor total é R$" + total + ", confirmar venda?",
confirm: function(button) {
    button.fadeOut(2000).fadeIn(2000);
    Aqui aparece normal > alert(total);
},
cancel: function(button) {
    button.fadeOut(2000).fadeIn(2000);
    alert("Venda não realizada.");
},
confirmButton: "SIM",
cancelButton: "NÃO"
});
the Alert I put is to check if the variable has any value, and shows the expected value, but in the sentence text:"O valor total é... the value does not appear. Any help is worth. vlw.

  • tried to change his statement with var before? Like this: var total = "";

  • 1

    I tried yes, I even tried to do the calculation in global scope but from the same error, the variable itself has value but is not returned at that specific position. other tests I did was to put a logo value in the variable declaration. ai works, but I need this value to be dynamic and not predefined.

  • Already tried to convert to integer before performing multiplication?

  • I tried now, by giving me the tip, thanks, but also did not work.

  • I don’t know this syntax ERRO >>>, put the code exactly the way you did and to comment lines use the /**/ or //

2 answers

2

put $(). confirm() inside the function, solved the problem in part. see how it turned out:

var preco = "";
var qtd = "";
var total = "";
 
function keypressed( obj , e , f) {
     var tecla = ( window.event ) ? e.keyCode : e.which;
     preco=f;
     qtd=tecla-48;
     total=qtd*preco;

    if ( tecla == 8 || tecla == 0 || tecla == 13)
        return true;
    if (tecla < 48 || tecla > 57 )
        {return false;}

$("#Confirma").confirm({
                title:"Confirmação",
Não atualiza>>      text:"O valor total é R$" + total + ", confirmar venda?",
                confirm: function(button) {
                    button.fadeOut(2000).fadeIn(2000);        
Atualiza normal >>      alert(total);
                },
                cancel: function(button) {
                },
                confirmButton: "SIM",
                cancelButton: "NÃO"
            });
        }

saw that the text: does not update a variable when triggered a second time, just takes the value the first time the button is triggered and even changing the input values the result does not change, to resolve this gave a refresh at a specific point on the page (button) and worked. This solution is not correct, an optimized solution is welcome. vlw personal.

1

Let’s at the moment ignore the code of each block, and see how the content is interpreted:

total = ""; // declare o valor de 'total'

function keypressed(obj, e, f) {} // declare uma função

$("#confirma").confirm({}); // invoque .confirm() via jQuery

Note that nothing in function declaration makes the code $("#confirma").confirm({}) wait before it is executed. The resulting then of course: . confirm is called before the value is changed.

A solution would be to embed the call $("#confirma").confirm({}) within a second function, called from the first.

Browser other questions tagged

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