With php I generate a form based on a multidimensional array. The form contains a number of input type=”text” fieldpairs that are named “items[level1][level2][price]” and “items[level1][level2][amount]”. I display these in a table.
Now, I want to add two things using jquery:
- an extra column that displays the total price for this item (=items[level1][level2][price].value * items[level1][level2][amount].value)
- At the bottom row: the total price for all items.
These values should update on every change-event of one of the mentioned textinputs.
I’ve been hitting a wall the last few hours, I hope someone here could give me some pointers.
Here’s a snippet of the generated html:
<form name="formname">
<table name="tablename">
<tr>
<td><input type="text" class="classname" name="items[1][2][amount]" /></td>
<td><input type="text" class="classname" name="items[1][2][price]" /></td>
<td>Here I want to display amount*price</td>
</tr>
<tr>
<td><input type="text" class="classname" name="items[1][8][amount]" /></td>
<td><input type="text" class="classname" name="items[1][8][price]" /></td>
<td>Here I want to display amount*price</td>
</tr>
<tr>
<td><input type="text" class="classname" name="items[3][4][amount]" /></td>
<td><input type="text" class="classname" name="items[3][4][price]" /></td>
<td>Here I want to display amount*price</td>
</tr>
...more rows like above...
<tr>Some more formelements that have nothing to do with the problem</tr>
<tr>
<td> </td>
<td> </td>
<td>Here I want to display the sum of all amount*price</td>
</tr>
</table>
</form>
There are a few ways you could do it. The basic idea is when the
.classnameinput changes, look for the parent row and then use that to look for the inputs in that row to multiply together and thetdto put the value it. You can look for them using a “name contains”[attr*=string]selector:Demo: http://jsfiddle.net/jtbowden/DJtTs/
Or if they are always the 1st/2nd column, use
.eq()or:eq():Demo: http://jsfiddle.net/jtbowden/DJtTs/1/
EDIT MARCO: