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

124
Views
Calcular la suma total de todos los números ingresados previamente en un campo

quiero calcular el total de los números introducidos por el usuario. Después de que un usuario haya agregado el nombre del artículo y la cantidad, quiero mostrar el total. ¿Cómo puedo hacer esto? Solo necesito mostrar el total.

Por ejemplo, nombre del artículo: 10 nombre del artículo: 5 total = 15

http://jsfiddle.net/81t6auhd/

 <body> <header> <h1>Exercise 5-2</h1> </header> <p>Item: <input type="text" id="item" size="30"> <p>Amount: <input type="text" id="amount" size="30"> <p><span id="message">*</span> <p><input type="button" id="addbutton" value="Add Item" onClick="processInfo();"> <script> var $ = function(id) { return document.getElementById(id); }; var myTransaction = []; function processInfo () { var myItem = $('item').value; var myAmount = parseFloat($('amount').value); var myTotal = myItem + ":" + myAmount; var myParagraph = $('message'); myParagraph.innerHTML = ""; myTransaction.push(myTotal); myParagraph.innerHTML += myTransaction.join("<br>"); }; (function () { $("addbutton").onclick = processInfo; })(); </script> </body>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

debe almacenar el valor anterior en algún lugar de la memoria para poder reutilizarlo en la próxima iteración

una propuesta puede ser almacenarla en el conjunto de datos del campo

 if ($('amount').dataset.previous) { myAmount += parseFloat($('amount').dataset.previous); } $('amount').dataset.previous = myAmount 

 var $ = function(id) { return document.getElementById(id); }; var myTransaction = []; function processInfo () { var myItem = $('item').value; var myAmount = parseFloat($('amount').value); if ($('amount').dataset.previous) { myAmount += parseFloat($('amount').dataset.previous); } $('amount').dataset.previous = myAmount; var myTotal = myItem + ":" + myAmount; var myParagraph = $('message'); myParagraph.innerHTML = ""; myTransaction.push(myTotal); myParagraph.innerHTML += myTransaction.join("<br>"); }; (function () { $("addbutton").onclick = processInfo; })();
 <p>Item: <input type="text" id="item" size="30"> <p>Amount: <input type="text" id="amount" size="30"> <p><span id="message">*</span> <p><input type="button" id="addbutton" value="Add Item" onClick="processInfo();">

about 4 years ago · Juan Pablo Isaza 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!