如何在 JavaScript 中添加多个数字?
我是 JavaScript 编程新手..我一直在研究解决方案,但仍然..没有运气。例如:我想添加用户输入的 6 个(或更多)数字。我使用这段代码,但只计算前三个。当我添加四个数字时,会出现“NAN”。 Nan的意思是无效计算。
<script type="text/javascript">
function show() {
var a = document.calc.B1.value*1;
var b = document.calc.B5.value*1;
var c = document.calc.B9.value*1;
var d = document.calc.B12.value*1;
var e = document.calc.B17.value*1;
var f = document.calc.B21.value*1;
document.calc.t1.value = a + b + c + d + e + f;
}
</script>
仅计算B1、B5和B9。这是工作代码:
<script type="text/javascript">
function show() {
var a = document.calc.B1.value*1;
var b = document.calc.B5.value*1;
var c = document.calc.B9.value*1;
document.calc.t1.value = a + b + c;
}
</script>
这是表单操作:
<form action="sysdocadd.php" method="post" name="calc">
t1= TOTAL (text type
<td><div align="center" class="style66"><input name="t1" type="text" size="18" id="t1" value="0.00"/></div></td>
当我单击计算按钮时,结果将显示在 t1 文本区域上。这是代码......
<tr>
<td><span class="style77">Click to add</span></td>
<td><div align="center" class="style66"><input type=button onClick='show()'value=Calculate /></div></td>
</tr>
请帮助我。 :(
这是 sysdocadd.php 代码:
<?php
$con = mysql_connect("localhost","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbconnect", $con);
$sql="INSERT INTO contents (reportnum, postedby, sysdate, userdateinp, B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, B11, B12,
B13, B14, B15, B16, B17, B18, B19, B20, B21, B22, B23, B24, B25, B26, B27, B28, B29, B30,
B31, B32, B33, B34, B35, B36, B37, B38, B39, B40, B41, B42, B43, B44, B45, B46, B47, B48, B49, B50, B51, B52,
B53, B54, B55, B56, t1, t2, t3, t4)
VALUES
('$_POST[reportnum]','$_POST[postedby]','$_POST[sysdate]','$_POST[userdateinp]','$_POST[B1]','$_POST[B2]','$_POST[B3]',
'$_POST[B4]','$_POST[B5]','$_POST[B6]','$_POST[B7]','$_POST[B8]','$_POST[B9]','$_POST[B10]','$_POST[B11]','$_POST[B12]',
'$_POST[B13]','$_POST[B14]','$_POST[B15]','$_POST[B16]','$_POST[B17]','$_POST[B18]','$_POST[B19]','$_POST[B20]','$_POST[B21]',
'$_POST[B22]','$_POST[B23]','$_POST[B24]','$_POST[B25]','$_POST[B26]','$_POST[B27]','$_POST[B28]','$_POST[B29]','$_POST[B30]',
'$_POST[B31]','$_POST[B32]','$_POST[B33]','$_POST[B34]','$_POST[B35]','$_POST[B36]','$_POST[B37]','$_POST[B38]','$_POST[B39]','$_POST[B40]',
'$_POST[B41]','$_POST[B42]','$_POST[B43]','$_POST[B44]','$_POST[B45]','$_POST[B46]','$_POST[B47]','$_POST[B48]','$_POST[B49]','$_POST[B50]',
'$_POST[B51]','$_POST[B52]','$_POST[B53]','$_POST[B54]','$_POST[B55]','$_POST[B56]','$_POST[t1]','$_POST[t2]','$_POST[t3]','$_POST[t4]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
I'm new to JavaScript Programming.. I've been researching for the solution but still.. no luck. E.g: I want to add like 6 numbers (or more) that the user will input. I use this code but only the first three are calculated. When I add like four numbers already, 'NAN' appears. Nan means invalid computation.
<script type="text/javascript">
function show() {
var a = document.calc.B1.value*1;
var b = document.calc.B5.value*1;
var c = document.calc.B9.value*1;
var d = document.calc.B12.value*1;
var e = document.calc.B17.value*1;
var f = document.calc.B21.value*1;
document.calc.t1.value = a + b + c + d + e + f;
}
</script>
Only B1, B5 and B9 are calculated. Here's the working code:
<script type="text/javascript">
function show() {
var a = document.calc.B1.value*1;
var b = document.calc.B5.value*1;
var c = document.calc.B9.value*1;
document.calc.t1.value = a + b + c;
}
</script>
Here's the form action:
<form action="sysdocadd.php" method="post" name="calc">
t1= TOTAL (text type
<td><div align="center" class="style66"><input name="t1" type="text" size="18" id="t1" value="0.00"/></div></td>
When I click calculate button, the result will be shown on t1 text area. here's the code for that..
<tr>
<td><span class="style77">Click to add</span></td>
<td><div align="center" class="style66"><input type=button onClick='show()'value=Calculate /></div></td>
</tr>
Please help me. :(
Here's the sysdocadd.php code:
<?php
$con = mysql_connect("localhost","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbconnect", $con);
$sql="INSERT INTO contents (reportnum, postedby, sysdate, userdateinp, B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, B11, B12,
B13, B14, B15, B16, B17, B18, B19, B20, B21, B22, B23, B24, B25, B26, B27, B28, B29, B30,
B31, B32, B33, B34, B35, B36, B37, B38, B39, B40, B41, B42, B43, B44, B45, B46, B47, B48, B49, B50, B51, B52,
B53, B54, B55, B56, t1, t2, t3, t4)
VALUES
('$_POST[reportnum]','$_POST[postedby]','$_POST[sysdate]','$_POST[userdateinp]','$_POST[B1]','$_POST[B2]','$_POST[B3]',
'$_POST[B4]','$_POST[B5]','$_POST[B6]','$_POST[B7]','$_POST[B8]','$_POST[B9]','$_POST[B10]','$_POST[B11]','$_POST[B12]',
'$_POST[B13]','$_POST[B14]','$_POST[B15]','$_POST[B16]','$_POST[B17]','$_POST[B18]','$_POST[B19]','$_POST[B20]','$_POST[B21]',
'$_POST[B22]','$_POST[B23]','$_POST[B24]','$_POST[B25]','$_POST[B26]','$_POST[B27]','$_POST[B28]','$_POST[B29]','$_POST[B30]',
'$_POST[B31]','$_POST[B32]','$_POST[B33]','$_POST[B34]','$_POST[B35]','$_POST[B36]','$_POST[B37]','$_POST[B38]','$_POST[B39]','$_POST[B40]',
'$_POST[B41]','$_POST[B42]','$_POST[B43]','$_POST[B44]','$_POST[B45]','$_POST[B46]','$_POST[B47]','$_POST[B48]','$_POST[B49]','$_POST[B50]',
'$_POST[B51]','$_POST[B52]','$_POST[B53]','$_POST[B54]','$_POST[B55]','$_POST[B56]','$_POST[t1]','$_POST[t2]','$_POST[t3]','$_POST[t4]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以稍微简化一下自己的答案:
You can simplify your own answer a little bit:
问题解决了。
谢谢你们。 :)
Problem's solved.
THANKS Y'ALL. :)