Problems with Jquery selectors

Asked

Viewed 43 times

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 the values of the fields named "proteina2" and "quantidade2" that loop in php showing values... What I need is not very difficult I believe, but as I am new in Jquery, I wanted help to better understand this part. I wanted to take the values of the "proteina2" and "quantity2" fields and sum them separately by taking all the values of the loop for these fields. For example, I have there:

 Proteina2 | quantidade 2

   5           10

   4           2

In this case what I need is to take the whole loop of "proteina2" and add with "quantity2" and generate the following result:

resultado: 15 

resultado: 6

2 answers

0

I got the result I wanted that 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>

0


Whoa, I think I can handle 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());
          }
       }
    });  
  • This way it will add up all proteins or all amounts. I wanted a way for him to add the first protein with the first quantity, the second protein with the second quantity. and so on.

  • Ai needs its HTML structure to see how it will do.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.