Alert does not appear

Asked

Viewed 182 times

2

Well, I’ve tried everything I know to fix this, but I couldn’t, so could someone help me? When clicking on the "Boleto" and selecting a quantity, clicking on the Finalize, an action would occur where it takes the amount that was chosen and multiplied by 32, then it should return to the "Return" and come in Alert. Follows the code:

function valorAPagar(){
	var j = 0;
	if(document.getElementById("forma1").checked==true){
		j = (document.getElementById("inte") * 32);
		return j;
    }
		alert(valorAPagar());
  }
<input type="checkbox" id="forma1" value="Boleto">Boleto<br>
<input type="checkbox" id="forma2" value="Cartão de Crédito">Cartão de Crédito<br>
Entrada Inteira: <input type="number" id="inte"><br>
Meia Entrada: <input type="number" id="mei"><br>
<input type="submit" value="Finalizar" onClick="valorAPagar()">`

  • The Return terminates the function and does not arrive in the Alert that comes after. If you get to Alert, you will loop infinitely because you are calling the function itself within it.

  • Right, so what could I do to have this Lert on the screen with the result(value) to pay?

2 answers

1


I see 2 basic problems in the code:

1) If the billet box is checked, it will never reach Alert because Return closes the function. And even if it does, it will generate an infinite loop, because Alert is inside the function and is calling the function itself.

2) Missing catch the value in document.getElementById("inte") to multiply by 32: document.getElementById("inte").value * 32.

The solution I see is to remove the return j and put alert(j):

function valorAPagar(){
   var j = 0;
   if(document.getElementById("forma1").checked==true){
      j = (document.getElementById("inte").value * 32);
   }
   alert(j);
}
<input type="checkbox" id="forma1" value="Boleto">Boleto<br>
<input type="checkbox" id="forma2" value="Cartão de Crédito">Cartão de Crédito<br>
Entrada Inteira: <input type="number" id="inte"><br>
Meia Entrada: <input type="number" id="mei"><br>
<input type="submit" value="Finalizar" onClick="valorAPagar()">

  • I’m sorry kskss, the bundle here put the tapeworm, but it didn’t save, damn headache already, thank you very much!

0

Essentially Voce is making an infinite loop at the end of valorAPagar Voce calls again valorAPagar that when it’s over it calls again valorAPagar, and so on.

Another problem was that when it came time to get the whole Qtd, it was wrong, the document.getElementById("inte") returns a Element, and not the value of the input, and that object has the property value that ai is the value of the input.

function valorAPagar(){
  var j = 0;
  if(document.getElementById("forma1").checked==true){
    j = (document.getElementById("inte").value * 32);
    alert('Voce vai pagar '+j);
  }
}
<input type="checkbox" id="forma1" value="Boleto">Boleto<br>
<input type="checkbox" id="forma2" value="Cartão de Crédito">Cartão de Crédito<br>
Entrada Inteira: <input type="number" id="inte"><br>
Meia Entrada: <input type="number" id="mei"><br>
<input type="submit" value="Finalizar" onClick="valorAPagar()">`

Browser other questions tagged

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