Multiply elements of the same jQuery class

-1

I have the following problem I need to multiply two values according to their class but I have more than two boxes where the values are to be multiplied, the multiplication nothing else does to me once here is my code

<div>
  <div class='mult'>
    <input type="number" class="unitario" id="unitario">
  </div>
  <div class='mult'>
    <input type="number" class="cantidad" id="cantidad">
  </div>
  <div>
    <input type="text" class="total" readonly>
  </div>
</div>

<div>
  <div class='mult'>
    <input type="number" class="unitario" id="unitario">
  </div>
  <div class='mult'>
    <input type="number" class="cantidad" id="cantidad">
  </div>
  <div>
    <input type="text" class="total" readonly>
  </div>
</div>

$(document).ready(function(){
      $(".mult input").keyup(multInputs);

    function multInputs() {
        $(".mult").each(function() {
            var $cantidad = $(".cantidad").val();
            var $unitario = $(".unitario").val();
            var $total = $cantidad * $unitario;
            $(".total").val($total);
        });
    }
});

Thank you in advance.

    
asked by Luis Miguel 16.08.2018 в 03:11
source

2 answers

1

What the Aztec says is true but if I understood you what you want to do, you can if you put the two inputs in the same div and then you position yourself with a jQuerry or getElement selector and search for your children by the index [0 ] and [1], then with a for loop you do the operation n times greetings ...

    
answered by 16.08.2018 в 06:50
0

RTFM: link

When selecting the value of a selector (Multiple in your case) only grabs the first incidence, osease var $cantidad , var $unitario and var $total will only work at the top of your HTML

    
answered by 16.08.2018 в 03:21