php - jQuery live calc multirow input
564
This is my code:
<?php
for($i=1;$i<10;$i++){
echo '<input type="text" >';
echo '<input type="text" >';
echo '<input type="text" disabled id="result'. $i .'"><p>';
}
echo '<input type="text" disabled id="total"><p>';
?>
and jQuery:
$(document).ready(function(){
$(".count").keyup(function(){
for (var i = 0; i < 10; i++) {
var val1 = +$(".value"+ i).val();
var val2 = +$("."+ i +"value").val();
$("#result" + i).val(val1*val2);
}
});
});
$(document).ready(function(){
$(".count").keyup(function(){
for (var i = 0; i < 10; i++) {
var vala = 0;
vala += +$("#result"+ i).val();
}
$("#total").val(vala);
});
});
First part of code works great.
Return multiplication two inputs toid=result$i.
I have a problem with last oneid=total.
It should return sum of all result X inputs
but now only return the last multiplication.
Do You have any idea what's wrong?
Answer
Solution:
You can simplify your code by grouping the related
input
elements together in a containingdiv
, using DOM traversal to retrieve the needed values, and joining the twofor
loops together. Try this:Answer
Solution:
That is because you have defined variable
vala
to 0 in for loop. which should be outside for loop: