javascript - Calculate only current field not changed fields -
i have tried couple of things cannot seem right! have following document (code follow), calculates automatically , add fields add more quantities. if qty_1 changed (manually), , calculation added, "changed qty_1" reverts first calculated value. changed value remain.
the file:
<script> function calculate(){ for(var i=1; i<=ct; i++){ var currentdiv = document.getelementbyid(i); var ori_qty = currentdiv.getelementsbytagname('input')[0].value; var total = math.round(ori_qty/0.8); currentdiv.getelementsbytagname('input')[1].value = total; currentdiv.getelementsbytagname('input')[2].value = total; }; } var ct = 1; function new_combi(){ ct++; var div1 = document.createelement('div'); div1.id = ct; // link delete extended form elements var dellink = '<div style="text-align:right;margin-right:65px"><a href="javascript:delit('+ ct +')">delete</a></div>'; div1.innerhtml = document.getelementbyid('var_roo_fields').innerhtml + dellink; document.getelementbyid('addcom').appendchild(div1); } function delit(eleid){ d = document; var ele = d.getelementbyid(eleid); var parentele = d.getelementbyid('addcom'); parentele.removechild(ele); } </script> <form id="form1" name="form1" method="post" action=""> <div id="addcom" style="width:100%"> <div id="1"> <table class='table1' width="100%" border="0" cellpadding="0" cellspacing="2"> <tr> <td width="25%" valign="top">original quantity</td> <td width="27%" valign="top"><input type="text" name="ori_qty[]" onblur="calculate()" /></td> <td width="24%" valign="top"> </td> <td width="24%" valign="top"> </td> </tr> <tr> <td valign="top">quantity 1</td> <td valign="top"><input type="text" name="qty_1[]" id="qty_1[]" /> </td> <td valign="top">quantity 2</td> <td valign="top"><input type="text" name="qty_2[]" id="qty_2[]" /></td> </tr> </table> </div> </div> <a href="javascript:new_combi()">add calculation</a> </form> <!-- repeat fields --> <div id="var_roo_fields" style="display:none"> <div> <hr /> <table class='table1' width="100%" border="0" cellpadding="0" cellspacing="2"> <tr> <td width="25%" valign="top">original quantity</td> <td width="27%" valign="top"><input type="text" name="ori_qty[]" onblur="calculate()" /></td> <td width="24%" valign="top"> </td> <td width="24%" valign="top"> </td> </tr> <tr> <td valign="top">quantity 1</td> <td valign="top"><input type="text" name="qty_1[]" id="qty_1[]" /> </td> <td valign="top">quantity 2</td> <td valign="top"><input type="text" name="qty_2[]" id="qty_2[]" /></td> </tr> </table> </div> </div>
so original amounts calculated - , changed manually - stay same after calculation added.
here have fiddle: https://jsfiddle.net/y8une7ub/
Comments
Post a Comment