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

165
Visualizações
how to add n number of input value with javascript

Friends, I want the sum of the numbers I put in fields and these input value total are equal to my total value. it show a Nan Error When i add these input values please help

This is my body Content

    <form action="" method="post">
    <input type="text" name="cnum" placeholder="enter no. of input">
    <input type="submit" name="submit" >
    </form>
    <?php
    if(isset($_POST['cnum']))
    {
        $cnum=$_POST['cnum'];
        $i=0;
        ?>
    <form action="" method="post">
        <input type="hidden" name="total" id="total" value="10">
        <input type="hidden" name="cnum" id="cnum" value="<?php echo $cnum ;?>">
        <?php
        while($i<$cnum)
        {
            ?>
                <input type="number" id="aq<?php echo $i; ?>" onKeyup="add()" name="aq<?php echo $i; ?>"><br />
            <?php
            $i++;
        }
    ?>
        <input type="submit" name="aqsub">
    </form>
    <?php
    }
    ?>

This is my java script function

var val=0;
var total=document.getElementById('total').value;
var cnum=document.getElementById('cnum').value;
var anum;
function add()
{
var n=0;
while(n<cnum)
{
    anum = 0;
    if(document.getElementById('aq'+n).value != "")        {            
            anum=parseInt(document.getElementById('aq'+n).value);
        }
    val += anum;
    n++;
}
alert(val);

}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Add Following code in your javascript

cnum = parseInt(cnum);
over 4 years ago · Santiago Trujillo Relatório

0

Update code as:

var total= parseInt(document.getElementById('total').value);

As you are using total to comparing with anum. so must be error Nan for total

over 4 years ago · Santiago Trujillo Relatório

0

Well I don't understand your logic but you get NAN because your value of fields are blank.

try this code

var val=0;
var total=document.getElementById('total').value;
var cnum=document.getElementById('cnum').value;
var anum;
function add()
{
    var n=0;
    while(n<cnum)
    {
        anum = 0;
        if(document.getElementById('aq'+n).value != "")        {            
                anum=parseInt(document.getElementById('aq'+n).value);
            }
        val += anum;
        n++;
    }
    if(total==anum)
    {
        alert('Its Work');
    }
    else
    {
        alert('Its not Work');
    }

}
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