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

0

The question title function only works by returning a type = range entry as number in the browser. While in Smarthphone only NaN returns, probably jquery.1.9.1.js or jquery-mobile.1.4.5.js is not supported, any suggestions?

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>
    
asked by anonymous 02.03.2017 / 04:13

1 answer

0

You should check if it's a number before you increment:

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><inputtype="number">
<input type="number">
<button onclick="validar();">Validar</button>
    
02.03.2017 / 12:12