Problems with Jquery selectors

1

My problem is this, I have this code:

<script language="javascript">
$(document).ready(function(){

    $('#simular').each(function(){
          $(this).click(function(){
                    
                    $("input[name=proteina2],input[name=quantidade2]").each(function(){  
                        var proteina2 = Number($(this).val());
                        if (!isNaN(proteina2)) prot = proteina2; 
                        alert(prot);
                    });    
                     
         });
    });

});
</script>

It takes values from fields named "protein2" and "quantity2" that loop in php showing values ... What I need is not too hard I believe, but since I'm new to Jquery, I wanted some help to understand this part better. I wanted to get the values from the field "protein2" and "quantity2" and add it up separately and get all the values of the loop for those fields. For example, I have there:

 Proteina2 | quantidade 2

   5           10

   4           2

In case what I need is to get the whole loop of "protein2" and add it with "quantity2" and it would generate the following result:

resultado: 15 

resultado: 6
    
asked by anonymous 13.04.2016 / 03:57

2 answers

0

So I think that solves it:

   $("input[name=proteina2],input[name=quantidade2]").each(function(){ 
       var quantidade2,proteina2 = 0;
       if($(this).attr('name') == 'proteina2'){
          if (!isNaN($(this).val())) {
             proteina2 += Number($(this).val());
          }
       }else{
          if (!isNaN($(this).val())) {
             quantidade2 += Number($(this).val());
          }
       }
    });  
    
13.04.2016 / 04:55
0

I got the result I wanted this way:

<script language="javascript">
  $(document).ready(function(){
   $('#simular').each(function(){
    $(this).click(function(){

                   var prot = 0;
                   var quant = 0;
                   var teste = 0;
                   var totalquanti = 0;
                   var a = [];
                   var b = [];

                $('input[name=proteina2]').each(function(){
                        var proteina = Number($(this).val());
                        if (!isNaN(proteina)) prot = proteina;
                        a.push(prot);
                });

                $('input[name=quantidade2]').each(function(){
                    var quantidade = Number($(this).val());
                    if (!isNaN(quantidade)) quant = quantidade;
                    b.push(quant);
                });

                for (i=0; i < a.length && i < b.length; i++){

                    if (a[i] != 0){
                        totalquanti += b[i]; 
                        conta = (a[i] * b[i])/100;
                        teste += conta;
                        nova = teste/totalquanti;
                    }
                }
                var totalconta = nova*100;
                var consegui = totalconta.toFixed(2);
                var final = "%";
                var end = consegui + final;

                $("#opa").html(end);

      });
   });
 });
</script>
    
16.04.2016 / 02:36