Aprenda neste post, como calcular desconto de um valor.
O desconto pode ser em porcentagem (%) ou em valor real (R$).
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Calcular Desconto</title> <script type="text/javascript"> function calcValor(){ // zerando total document.getElementById("total").value = '0'; // valor lÃquido var VTOTALLIQUIDO = parseFloat(document.getElementById("valor1").value); // desconto 1 - porcentagem var DESCONTO1 = parseFloat(document.getElementById("desconto1").value); var PDESCONTO = parseFloat( (VTOTALLIQUIDO * DESCONTO1)/100 ); // desconto 2 - valor var VDESCONTO = parseFloat(document.getElementById("desconto2").value); var TOTAL = parseFloat(VTOTALLIQUIDO) - parseFloat(PDESCONTO) - parseFloat(VDESCONTO); //document.getElementById("h1").value = 'R$ ' + PDESCONTO.toFixed(2); //document.getElementById("h2").value = 'R$ ' + VDESCONTO.toFixed(2); document.getElementById("total").value = 'R$ ' + TOTAL.toFixed(2); } </script> <style type="text/css" media="all"> table tr td { padding: 5px; } table tr td.dir { padding-right: 15px; text-align: right; width: 120px; } </style> </head> <body> <p>Calcular valor do desconto:</p> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td class="dir">campo 1:</td> <td><input type="text" name="valor1" id="valor1" value="550.25" /></td> </tr> <tr> <td class="dir">desconto %:</td> <td><input type="text" name="desconto1" id="desconto1" /></td> </tr> <tr><td colspan="2"><!--<input type="text" name="h1" id="h1" />--></td></tr> <tr> <td class="dir">desconto R$:</td> <td><input type="text" name="desconto2" id="desconto2" onblur="calcValor()" /></td> </tr> <tr><td colspan="2"><!--<input type="text" name="h2" id="h2" />--></td></tr> <tr> <td class="dir">valor total:</td><td><input type="text" name="total" id="total" /></td> <tr> </table> </body> </html>
Explicando:
O função que fará o cálculo é chamada no evento onblur do campo “desconto R$:”.
Related posts:

