如何在 JavaScript 中添加多个数字?

发布于 2024-10-24 02:48:31 字数 3102 浏览 2 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄居人 2024-10-31 02:48:31

您可以稍微简化一下自己的答案:

function addNums() {
    var sum = 0;

    for(i=0; i<14; i++)
        sum += parseFloat(document.forms["addition"]["B" + (4*i+1)].value);

    document.forms["addition"].t1.value = sum;
}

You can simplify your own answer a little bit:

function addNums() {
    var sum = 0;

    for(i=0; i<14; i++)
        sum += parseFloat(document.forms["addition"]["B" + (4*i+1)].value);

    document.forms["addition"].t1.value = sum;
}
捂风挽笑 2024-10-31 02:48:31

问题解决了。

<script language="javascript" type="text/javascript">

function addNums(){
  num_1=Number(document.addition.B1.value);
  num_2=Number(document.addition.B5.value);
  num_3=Number(document.addition.B9.value);
  num_4=Number(document.addition.B13.value);
  num_5=Number(document.addition.B17.value);
  num_6=Number(document.addition.B21.value);
  num_7=Number(document.addition.B25.value);
  num_8=Number(document.addition.B29.value);
  num_9=Number(document.addition.B33.value);
  num_10=Number(document.addition.B37.value);
  num_11=Number(document.addition.B41.value);
  num_12=Number(document.addition.B45.value);
  num_13=Number(document.addition.B49.value);
  num_14=Number(document.addition.B53.value);
  valNum=num_1+num_2+num_3+num_4+num_5+num_6+num_7+num_8+num_9+num_10+num_11+num_12+num_13+num_14;
  document.addition.t1.value=valNum;
}
</script>

谢谢你们。 :)

Problem's solved.

<script language="javascript" type="text/javascript">

function addNums(){
  num_1=Number(document.addition.B1.value);
  num_2=Number(document.addition.B5.value);
  num_3=Number(document.addition.B9.value);
  num_4=Number(document.addition.B13.value);
  num_5=Number(document.addition.B17.value);
  num_6=Number(document.addition.B21.value);
  num_7=Number(document.addition.B25.value);
  num_8=Number(document.addition.B29.value);
  num_9=Number(document.addition.B33.value);
  num_10=Number(document.addition.B37.value);
  num_11=Number(document.addition.B41.value);
  num_12=Number(document.addition.B45.value);
  num_13=Number(document.addition.B49.value);
  num_14=Number(document.addition.B53.value);
  valNum=num_1+num_2+num_3+num_4+num_5+num_6+num_7+num_8+num_9+num_10+num_11+num_12+num_13+num_14;
  document.addition.t1.value=valNum;
}
</script>

THANKS Y'ALL. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文