Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

211
Views
Intentando generar valor de un método que es casi idéntico a otro método, pero este no funciona

Lo que quiero que haga mi programa
Cuando ingreso el código de descuento correcto ("descuento"), se descontará el 10 % del precio total y se mostrará el nuevo "total general". Si no se ingresa un código de descuento o se ingresa uno correcto, el total general será el mismo que el 'precio total'.

Problema
No se muestra nada en total. Esta es mi salida cuando hago clic en enviar con el código de descuento correcto ingresado: [Imagen de salida][1] [1]: https://i.stack.imgur.com/WUwnK.png

lo que he probado
En mi script JS he intentado lo siguiente:

 <script> function getGrandTotal(){ const total = document.getElementbyId("total").value; const dis = document.getElementbyId("discountCode").value; if(dis === "discount"){ document.getElementById("grandTotal").value= total * 0.9; }else{ grandTotal = total; } } document.getElementById('btnSubmit').addEventListener('click', getGrandTotal); </script>

Mi monto total fue asignado en esta función:

 <script> function getTotalAmount() { const choice = document.getElementById("burgerSize").value; const amount = document.getElementById("quantity").value; if (choice === "4") { document.getElementById("total").value = amount * 4; } if (choice === "6") { document.getElementById("total").value = amount * 6; } if (choice === "10") { alert("Health Warning"); document.getElementById("total").value = amount * 10; } } document.getElementById('burgerSize').addEventListener('change', getTotalAmount); document.getElementById('quantity').addEventListener('change', getTotalAmount); </script>

Estaba pensando que la misma estructura de método se aplicaría a ambas tareas. También intenté usar if(dis !== "discount"){grandTotal = total;} en lugar de mi declaración else, pero esto tampoco funciona. ¡Cualquier ayuda sería genial!

Aquí está mi código html relevante:

 <label> Total cost: </label> <input type="text" id="total" /><br> <label for = "discountCode"> Discount code: </label> <input type="text" id="discountCode" /><br> <input type="button" id="btnSubmit" value="Submit " onclick="return getGrandTotal()" /><br> <label> Grand total: </label> <input type="text" id="grandTotal" /><br>
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

solo algunos errores de sintaxis.

 <input type="button" id="btnSubmit" value="Submit " onclick="return getGrandTotal" /><br>

cambiar a

 onclick="getGrandTotal()"

document.getElementById distingue entre mayúsculas y minúsculas

código de trabajo aquí

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!