[英]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 当我添加这些输入值时,它会显示出Nan错误,请帮助
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 这是我的Java脚本功能
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);
}
在JavaScript中添加以下代码
cnum = parseInt(cnum);
Update code as: 将代码更新为:
var total= parseInt(document.getElementById('total').value);
As you are using total
to comparing with anum
. 当您使用total
与anum
进行比较时。 so must be error Nan for total 所以一定是错误南总
Well I don't understand your logic but you get NAN because your value of fields are blank. 好吧,我不理解您的逻辑,但您会得到NAN,因为您的字段值为空。
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');
}
}
why dont you directly include the PHP var into a JS var? 为什么不直接将PHP变量包含到JS变量中?
var cnum=<?php echo $cnum ;?>;
Also may check against "", and then use a zero 也可以对照“”,然后使用零
parseInt(input.value)||0;
Update JS part: 更新JS部分:
var total=document.getElementById('total').value;
total = parseInt(total);
var cnum=document.getElementById('cnum').value;
cnum= parseInt(cnum);
function add()
{
var anum;
var val=0;
var n=0;
while(n<cnum)
{
anum = 0;
var temp = document.getElementById('aq'+n).value;
if( temp != "") {
anum=parseInt(temp);
}
val = +val + anum;
n++;
}
alert(val);
}
var total=document.getElementById('total').value; total = parseInt(total); var cnum=document.getElementById('cnum').value; cnum= parseInt(cnum); function add() { var anum; var val=0; var n=0; while(n<cnum) { anum = 0; var temp = document.getElementById('aq'+n).value; if( temp != "") { anum=parseInt(temp); } val = +val + anum; n++; } alert(val); }
<form action="" method="post"> <input name="total" id="total" value="10" type="hidden"> <input name="cnum" id="cnum" value="10" type="hidden"> <input id="aq0" onkeyup="add();" name="aq0" value="" type="number"><br> <input id="aq1" onkeyup="add();" name="aq1" value="" type="number"><br> <input id="aq2" onkeyup="add();" name="aq2" value="" type="number"><br> <input id="aq3" onkeyup="add();" name="aq3" value="" type="number"><br> <input id="aq4" onkeyup="add();" name="aq4" value="" type="number"><br> <input id="aq5" onkeyup="add();" name="aq5" value="" type="number"><br> <input id="aq6" onkeyup="add();" name="aq6" value="" type="number"><br> <input id="aq7" onkeyup="add();" name="aq7" value="" type="number"><br> <input id="aq8" onkeyup="add();" name="aq8" value="" type="number"><br> <input id="aq9" onkeyup="add();" name="aq9" value="" type="number"><br>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.