简体   繁体   中英

Sum of form input in Javascript

I want the user to enter a number then when it is submitted, it is inserted into the array totalBags. The user can then submit another number, when submitted the array elements are added together. Eg Input = 2 Press submit Output = 2 New input = 3 Press submit Output = 5

Here is my code:

<html>
<head>

<script type = "text/javascript">

function submitOrder()
{
var allBags = [];
var bags_text = document.getElementById("bags").value;
allBags.push(bags_text);
var totalBags = 0;

for (var i = 0; i < allBags.length; i++)
{
    totalBags += allBags[i]; // here is the problem... i think
}
document.getElementById("container").innerHTML = "<p>"+totalBags+"</p><input type=\"reset\" value=\"New Order\" onClick=\"resetOrder()\" />";
}
function resetOrder()
{   
document.getElementById("container").innerHTML = "<p><label for=\"bags\">No. bags: </label><input type=\"text\" id=\"bags\" /></p><p><input type=\"button\" value=\"Subit order\" onClick=\"submitOrder()\"> <input type=\"reset\" value=\"Reset Form\" /></p>";
}
</script>
</head>
<body>
<form name="order_form" id="order_form">
<div id="container">
<label>Total bags: </label><input id="bags" type="text" ><br>
<input type="button" id="submitButton" value="Subit order" onClick="submitOrder()">
<input type="reset" value="Reset" class="reset" />
</div>
</form>
</html>

Try to use:

for (var i = 0; i < allBags.length; i++)
{
    totalBags += parseInt(allBags[i],10); 
}

Or use Number(allBags[i]) if you prefer that.

Your element allBags[i] is a string and + between strings and concatenting them.

Further study: What is the difference between parseInt(string) and Number(string) in JavaScript?

I should rewrite the program a bit. First, you can define global variables which won't be instantiated in the function. You are doing that, which resets the variables. Fe

function submitOrder()
{
    var allBags = [];
    // ...
}

It means that each time you're clicking on the button allBags is created as a new array. Then you add an value from the input element. The result is that you have always an array with one element. It's best to declare it outside the function. By this, you ensure that the variables are kept.

// general variables
var allBags = [];
var totalBags = 0;

function submitOrder()
{
    // the result is a string. You have to cast it to an int to ensure that it's numeric
    var bags_text = parseInt(document.getElementById("bags").value, 10);
    // add result to allBags
    allBags.push(bags_text);
    // total bags
    totalBags += bags_text;

    // display the result
    document.getElementById("container").innerHTML = "<p>"+totalBags+"</p><input type=\"reset\" value=\"New Order\" onClick=\"resetOrder()\" />";
}

by leaving out the loop, you have an more performant program. But don't forget to clear the array and the totalBags variable to 0 if you're using the reset button.

function resetOrder()
{   
    document.getElementById("container").innerHTML = "...";
    // reset variables
    totalBags = 0;
    allBags = [];
}
function submitOrder()
{
var allBags = parseInt(document.getElementById("bags").value.split(""),10);//Number can also used
var totalBags = 0;

for (var i = 0; i < allBags.length; i++)
{
    totalBags += allBags[i]; 
}
document.getElementById("container").innerHTML = "<p>"+totalBags+"</p><input type=\"reset\" value=\"New Order\" onClick=\"resetOrder()\" />";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM