Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

305
Visualizações
Find sum of automatically generated input fields value

I have a button that when you click it appends an input fields in a div, this values comes from ajax request.

 var i = 0;

 $(document).on('click', '#add-btn', function() {
     ++i;
           var user = $('#productName').attr('value');
           var price = $('#price').attr('value');    
      $.ajax({    //create an ajax request to display.php
        type: "GET",
        url: "getPrice.php", 
        data: {user:user, price:price},            
        dataType: "html",   //expect html to be returned                
        success: function(response){                    
          $("#dynamicAddRemove").append('<tr style="height:5em;" id="tr"><td><input name="productName['+i+']" readonly class="form-control" type="text" value="'+user+'"/></td><td><div class="quantit buttons_added"><input class="form-control quantity" id="number"  type="number" name="qty[' + i + ']" value="1"/></div></td><td><input  class="form-control amount" type="text" readonly value="'+price+'" name="price[' +i +']"/></td><td class="td"><input type="text" value="'+price+'" name="subTotal['+i+']" placeholder="Subtotal" class="form-control subtotal" id="grandTotal" readonly/><td><button type="button" name="add" class="btn btn-danger remove-tr">-</button></td>');  
        }

    });
});

after appending the input fields now i want to add every value of the subTotal to get grandTotal.

     var iSum = 0;
   $(document).on('mouseout', '.amount , .quantity, #addvalue', function() {
    iSum = 0;
       $('input[name="subTotal[]"]').each(function(){
        var LiSum =parseInt($('input[name="subTotal[]"]').val());
        if(LiSum != undefined || LiSum != ''){
            iSum+=LiSum;
        }
       
       });
       $('#sum').html(iSum);
       alert(iSum);
    });

after successfully appending values, i keep on getting 0 as grandTotal

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Replace $('input[name="subTotal[]"]') by $('input[name^="subTotal"]'). This will find all input fields with names starting with "subTotal". The name "subTotal[]" you were originally looking for does not appear anywhere in your markup.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda