2

forexample, i have this code (results from a php script loop): <input type="text" name="qtty[]" id="qtty[]" onFocus="startCalc();" onblur="stopCalc();"> <input type="hidden" name="price[]" id="price[]"> <input type="text" name="totalprice[]" id="totalprice[]">

And this for javascript:

function startCalc(){
    interval = setInterval("calc()",500);
}

function calc(){
$('input[name="qtty[]"]').each(function(){
    qtty    = $(this).val();
});
$('input[name="price[]"]').each(function(){
    price   = $(this).val();
});
total   = (qtty * 1) * (price * 1);
$('input[name="totalprice[]"]').val(total);

}


function stopCalc(){
    clearInterval(interval);
}

The moment I enter the first input to the array, the program does not show anything. but at the time of the second array of data fed, TotalPrice will change both

Here, Example pict:

  1. http://s7.postimg.org/memsupuh7/Capture1.png
  2. http://s23.postimg.org/6rdfk2rzf/Capture.png
ANDY
  • 43
  • 6

2 Answers2

1

I think you are in the wrong way. This is more preferred variant:

<input type="text" name="qtty">
<input type="hidden" name="price" value="2">
<input type="text" name="totalprice">
<br>
<input type="text" name="qtty">
<input type="hidden" name="price" value="3">
<input type="text" name="totalprice">

and

$('input[name="qtty"]').keyup(function() {
  var qtty = $(this).val();
  var price = $(this).next().val(); 
  var total = (qtty * 1) * (price * 1);
  $(this).nextAll().eq(1).val(total); 
});

fiddle

raskalbass
  • 741
  • 4
  • 18
  • But why if i use table for place that textfield it didn't work ? http://jsfiddle.net/andy_yatma_s/xq2Cu/6/ – ANDY Jun 10 '14 at 11:33
  • this is because of this expression : `$(this).nextAll().eq(1).val(total);` it is mean: second element after this or simply totalprise input, but you put it in other `` so it is not works. – raskalbass Jun 10 '14 at 11:39
  • use for example this `$(this).parent().next().children().val(total);` http://jsfiddle.net/xq2Cu/7/ – raskalbass Jun 10 '14 at 11:48
  • but if you change structure of table - you must rewrite this rule – raskalbass Jun 10 '14 at 11:49
  • 1
    Wow... it works. Thanks dude, you just save my life :D – ANDY Jun 10 '14 at 11:52
  • You are welcome, but don't forget to edit nesting if you will rebuild the table or just add another element – raskalbass Jun 10 '14 at 11:59
0

try this

Assign the default value initially

    var qtty=0;
    var price =0;
    var interval ;
        function startCalc(){
            interval = setInterval(calc,500);
        }

        function calc(){
        $('input[name="qtty[]"]').each(function(){
            qtty    = $(this).val();
        });
        $('input[name="price[]"]').each(function(){
            price   = $(this).val();
        });
        total   = (qtty * 1) * (price * 1);
        $('input[name="totalprice[]"]').val(total);

        }


        function stopCalc(){
            clearInterval(interval);
        }
Balachandran
  • 9,277
  • 1
  • 13
  • 25