Input('type=number'). each(Function()) )}; does not recognize value only returns Nan

Asked

Viewed 132 times

0

The question title function only works by returning an entry of type=range as a number in the browser. Already on Smarthphone only returns Nan, probably the jquery.1.9.1.js or jquery-mobile.1.4.5.js is not compatible, some suggestion?

JS Code

<script>
     validar = function() {
         var soma = 0;
         $('input[type=number]').each(function() { //number devido ao código gerado pelo jquery mobile.
             soma += parseInt($(this).val());
         });
         console.log(soma);
         if (soma < 45) {
            location.href = "sua_pagina";
         }
         else{
             location.href="sua_pagina"
         }

     }      
 </script>

1 answer

0


You should check if it’s number before incrementing:

validar = function() {
         var soma = 0;
         $('input[type=number]').each(function() { //number devido ao código gerado pelo jquery mobile.
             var val = parseInt($(this).val());
             if(!isNaN(val)) {
                 soma += parseInt(val);
             }
         });
         console.log(soma);
         if (soma < 45) {
            //location.href = "sua_pagina";
         }
         else{
             //location.href="sua_pagina"
         }

     }    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="number">
<input type="number">
<button onclick="validar();">Validar</button>

  • It all worked out! The code works perfectly! I thank you very much friend!!

  • You’re welcome @Marcílioaguiar

Browser other questions tagged

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