javascript - Computing for the value of the last column cell -- involves dynamic table rows -


consider following html table:

<table id="mytable1">     <tr id="tr1">         <td><input type="text" id="quantity1" name="quantity1" /></td>         <td><input type="text" id="weight1" name="weight1" /></td>         <td><input type="text" id="sub_total1" name="sub_total1" /></td>     </tr> </table> 

what i'm trying accomplish here need update value sub_total field on every row based on values typed in quantity , weight fields on same row every time keyup() triggered.

now believe manageable task if table i'm working on static only. inclusion of dynamic adding of table rows caused me troubles.


jquery dynamic addition of rows:

$(document).ready(function() {  var counter = 2;  $("#addbutton").click(function() {      $('#mytable1 tr:last').after(         '<tr id="tr"><td><input type="text" name="quantity' + counter +              '" id="quantity' + counter + '" value=""></input></td>' +          '<td><input type="text" name="weight' + counter +              '" id="weight' + counter + '" value=""></input></td>' +          '<td><input type="text" name="sub_total' + counter +              '" id="sub_total' + counter + '" value=""></input></td></tr>'     );      counter++; }); }); 

here have formula used in computing sub_total:

var sub_total  = ((170 * first 10 kilos) + (70 * remaining weight)) * (quantity); 

so given sample values: quantity = 10 weight = 15, should have

var sub_total  = ((170 * 10) + (70 * 5)) * (10); 

i have following start ' not quite sure place inside these functions

$('#mytable1 input[id^=\'quantity\'], #mytable1 input[id^=\'weight\']').live('keyup',function() {      $('#mytable1 input[id^=\'quantity\'], #mytable1 input[id^=\'weight\']').each(function(index, value) {     }); }); 

you can use closest() match parent table row of modified element. there, it's easier locate quantity, weight , sub_total elements:

$("#mytable1 [id^=quantity], #mytable1 [id^=weight]").live("keyup", function() {     var $tr = $(this).closest("tr");     var quantity = parseint($("[id^=quantity]", $tr).val());     var weight = parseint($("[id^=weight]", $tr).val());      var firstten;     if (weight <= 10) {         firstten = weight;         weight = 0;     } else {         firstten = 10;         weight -= 10;     }      $("[id^=sub_total]", $tr).val((170 * firstten + 70 * weight) * quantity); }); 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -