jquery - realtime update value in form from table -
i have created table calculate grand total of few fields in real time .i have form after table input field named amount ,how can make value updated in real time same value grand total in table. meaning if grand total in table 10 value in form should change 10.
see fiddle: http://jsfiddle.net/hsxkw/48/
html:
<table> <thead> <tr> <th>variable</th> <th>qty</th> <th>price</th> <th>sum</th> </tr> </thead> <tbody> <tr> <td>millage</td> <td><input class='qty' size='1'/></td> <td class='price'>1.25</td> <td class='sum'>0</td> </tr> <tr> <td> number of vans</td> <td><input class='qty' size='1'/></td> <td class='price'>2.10</td> <td class='sum'>0</td> </tr> <tr> <td colspan='2'>total</td> <td id='total'></td> </tr> </tbody> </table> <form action=""> <input name="amount" value="0.00"> </form>
jquery:
function gettotal(){ var total = 0; $('.sum').each(function(){ total += parsefloat(this.innerhtml) }); $('#total').text(total); } gettotal(); $('.qty').keyup(function(){ var parent = $(this).parents('tr'); var price = $('.price', parent); var sum = $('.sum', parent); var value = parseint(this.value) * parsefloat(price.get(0).innerhtml||0); sum.text(value); gettotal(); })
right after
$('#total').text(total);
you can
$("form input[name='amount']").val(total);
Comments
Post a Comment