variable undefined

2

Hello,

I'm having a problem retrieving the global variable total in a script. In an alert inserted in the same scope it is displayed normal, but in a statement it is not returned, undefined appears. 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 placed is to check if the variable has any value, and it shows the expected value, but in the phrase text:"O valor total é... the value does not appear. Any help is worth it. vlw.     
asked by anonymous 27.10.2015 / 15:41

2 answers

2

I put the $ (). confirm () inside the function, solved the problem in part. Here's how it went:

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"
            });
        }

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

    
28.10.2015 / 13:50
1

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 the function declaration causes the $("#confirma").confirm({}) code to wait before it runs. The resulting then is clear: .confirm is called before the value is changed.

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

    
28.10.2015 / 14:09