2

I want to display the number of values ​​in array form. I have a lot of form with only one name is "price []" and have each value on the form. I want to calculate the total value of the form "price []". I'm still a beginner in javascript. and I want to calculate it by using javascript. The following coding that I have made.

<html>
    <body>
        <form id="hitung" name="hitung">
            price 1 <input type="text" name="price[]" class="price" value="1000"/><br>
            price 2 <input type="text" name="price[]" class="price" value="3000"/><br>
            price 3 <input type="text" name="price[]" class="price" value="2000"/><br>
            price 4 <input type="text" name="price[]" class="price" value="1000"/><br>
            price 5 <input type="text" name="price[]" class="price" value="3000"/><br><br>
            total <input type="text" name="total" class="total"/>
        </form>
    </body>
</html>

and I want to ask you again, if there is a new input from the outside, then the total will also be changed directly. how to do it. help me please. thank you

1
  • 1
    don't you need a quantity to go with price? Commented Dec 28, 2012 at 0:12

1 Answer 1

2

Here's an example: http://jsfiddle.net/c4UyA/1/

var sum = 0;
$("form > input[name='price[]']").each(function() {
    $this = $(this);
    sum += parseInt($this.attr("value"));
});
$("form > input[name='total']").attr("value", sum);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.