Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用JavaScript计算总数_Javascript - Fatal编程技术网

用JavaScript计算总数

用JavaScript计算总数,javascript,Javascript,OP,请用问题的详细描述替换此文本。你的代码在下面 我使用了document.getElementById,但数学不起作用。我需要计算总数: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">

OP,请用问题的详细描述替换此文本。你的代码在下面


我使用了
document.getElementById
,但数学不起作用。我需要计算总数:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
<script type = "text/javascript">
function a()
{

var q = document.getElementById('ad').value;
document.getElementById('s').value=q  + q;
}

</script>
</head>
<?php 
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("form1", $con);


error_reporting(E_ALL ^ E_NOTICE);
$nam=$_GET['msg'];

 $row=mysql_query("select * from inv where Name='$nam'");
while($row1=mysql_fetch_array($row))
{ 
$Name=$row1['Name'];
  $Address =$row1['Address'];
  $City=$row1['City'];
    $Pincode=$row1['Pincode'];
  $No=$row1['No'];
  $Date=$row1['Date'];
  $DCNo=$row1['DCNo'];
  $DcDate=$row1['DcDate'];
  $YourOrderNo=$row1['YourOrderNo'];
  $OrderDate=$row1['OrderDate'];
  $VendorCode=$row1['VendorCode'];
  $SNo=$row1['SNo'];
  $descofgoods=$row1['descofgoods'];
  $Qty=$row1['Qty'];
  $Rate=$row1['Rate'];
  $Amount=$row1['Amount'];
}

?>
<body>
<form id="form1" name="form1" method="post" action="">
  <table width="846" border="0">
    <tr>
      <td width="411" height="113">&nbsp;</td>
      <td width="412">&nbsp;</td>
    </tr>
  </table>
  <table width="846" border="0">
    <tr>
      <td height="38">&nbsp;</td>
    </tr>
  </table>
  <table width="846" border="0">

    <tr>
      <td width="390" rowspan="4">&nbsp;</td>
      <td width="92" height="35">&nbsp;</td>
      <td width="136"><?php echo $No;?></td>
      <td width="36">&nbsp;</td>
      <td width="170"><?php echo $Date;?></td>
    </tr>
    <tr>
      <td height="37">&nbsp;</td>
      <td><?php echo $DCNo;?></td>
      <td>&nbsp;</td>
      <td><?php echo $DcDate;?></td>
    </tr>
    <tr>
      <td height="34">&nbsp;</td>
      <td><?php echo $YourOrderNo;?></td>
      <td>&nbsp;</td>
      <td><?php echo $OrderDate;?></td>
    </tr>
    <tr>
      <td height="29">&nbsp;</td>
      <td><?php echo $VendorCode;?></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>
  <table width="845" border="0">
    <tr>
      <td height="38">&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="34">&nbsp;</td>
      <td width="457">&nbsp;</td>
      <td width="104">&nbsp;</td>
      <td width="79">&nbsp;</td>
      <td width="149">&nbsp;</td>
    </tr>
      <?php $i=1;  
    $row=mysql_query("select * from inv where Name='$nam'");
while($row1=mysql_fetch_array($row))
{ 
$descofgoods=$row1['descofgoods'];
  $Qty=$row1['Qty'];
  $Rate=$row1['Rate'];
  $Amount=$row1['Amount'];
?>
            <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $descofgoods;?></td>
              <td><?php echo $Qty;?></td>
              <td><?php echo $Rate;?></td>
              <td><input name="Amount" type = "text" id ="ad" value="<?php echo $Amount;?>" /></td>
            </tr>   


    <?php  $i++;} ?>
  </table>
  <table width="844" border="0">
    <tr>
      <td width="495" height="1065">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        <input type="text" name="textfield2" />        &nbsp; &nbsp; &nbsp; &nbsp; </td>
      <td width="191">&nbsp;</td>
      <td width="144"><input type="text" name="tot" id="s" onclick="a()"; /></td>
    </tr>

函数a()
{

var q=document.getElementById('ad')。值; document.getElementById('s')。value=q+q; }
您可能将字符串连接在一起,因此从
1+1
获得
11
。尝试使用
parseInt()
将字符串转换为数字,如下所示:

var q = parseInt(document.getElementById('ad').value, 10);
document.getElementById('s').value = q + q;

更新:正如CMS在另一个答案中指出的那样,您还生成了具有相同ID的多个元素,这是无效的,除此之外,
getElementById()
将只返回一个元素。请查看解决此问题的一种方法


此外,请注意,如果要将十进制数相加,则不能按上述建议使用
parseInt()
。您可以使用
parseFloat()
或一元
+
运算符将字符串转换为数字。尽管如此,您必须这样做。

您可能将字符串连接在一起,因此从
1+1
中获得
11
。尝试使用
parseInt()
将字符串转换为数字,如下所示:

var q = parseInt(document.getElementById('ad').value, 10);
document.getElementById('s').value = q + q;

更新:正如CMS在另一个答案中指出的那样,您还生成了具有相同ID的多个元素,这是无效的,除此之外,
getElementById()
将只返回一个元素。请查看解决此问题的一种方法

此外,请注意,如果要将十进制数相加,则不能按上述建议使用
parseInt()
。您可以使用
parseFloat()
或一元
+
运算符将字符串转换为数字。尽管如此,你还是必须这样。

我知道你有:

id ="ad" value="<?php echo $Amount;?>"
我知道你有:

id ="ad" value="<?php echo $Amount;?>"

您不能使用document.getElementById('ad'),因为这只会返回一个元素(如果SQL查询只返回一行,则该操作有效)

请改用document.getElementsByName('Amount')(注意,该参数区分大小写),因为它将返回一个名为Amount的多个元素数组,您可以迭代并计算总数

您需要更改Javascript函数a()。见下文

function a()
{
    var q = document.getElementsByName('Amount');
    var count = 0;
    for (i = 0; i < q.length; i++)
        count += parseInt(q[i].value,10);
    document.getElementById('s').value = count;
}
函数a()
{
var q=document.getElementsByName(“金额”);
var计数=0;
对于(i=0;i
您不能使用document.getElementById('ad'),因为这只会返回一个元素(如果SQL查询只返回一行,则该操作有效)

请改用document.getElementsByName('Amount')(注意,该参数区分大小写),因为它将返回一个名为Amount的多个元素数组,您可以迭代并计算总数

您需要更改Javascript函数a()。见下文

function a()
{
    var q = document.getElementsByName('Amount');
    var count = 0;
    for (i = 0; i < q.length; i++)
        count += parseInt(q[i].value,10);
    document.getElementById('s').value = count;
}
函数a()
{
var q=document.getElementsByName(“金额”);
var计数=0;
对于(i=0;i
除了其他人已经解决的类型转换问题之外,通过查看您的代码,我看到了一些问题:

  • 通过在循环中创建具有相同id(
    id=“ad”
    )的多个
    input
    元素,您正在生成无效标记
  • document.getElementById
    只返回一个元素
  • 我建议您输入一个
    名称,以标识要求和的
    输入
    元素:

    ...
    <td>
      <input class="amount" type="text" value="<?php echo $Amount;?>" />
    </td>
    ...
    
    。。。
    
    除了其他人已经解决的类型转换问题之外,通过查看您的代码,我看到了一些问题:

  • 通过在循环中创建具有相同id(
    id=“ad”
    )的多个
    input
    元素,您正在生成无效标记
  • document.getElementById
    只返回一个元素
  • 我建议您输入一个
    名称,以标识要求和的
    输入
    元素:

    ...
    <td>
      <input class="amount" type="text" value="<?php echo $Amount;?>" />
    </td>
    ...
    
    。。。
    
    var q=document.getElementById('ad')。值-0;这也行。parseInt(string,radix)可能是执行此操作的正确方法。var q=document.getElementById('ad')。值-0;这也行。parseInt(字符串,基数)可能是正确的方法。@DAFFODIL请在你的问题上多加努力。1) 请在此处发布代码,而不是在第三方网站上。当代码从dpaste中删除后的一个月内,这个问题对于其他人来说将毫无价值。2) 专注于代码的相关部分。3) 描述问题。“nt有效”是一个问题描述。@水仙花请在你的问题上多加努力。1) 请在此处发布代码,而不是在第三方网站上。当代码从dpaste中删除后的一个月内,这个问题对于其他人来说将毫无价值。2) 专注于代码的相关部分。3) 描述问题。“Is nt working”是一个问题描述。似乎OP需要对货币金额求和,
    parseFloat
    或一元加号运算符是比
    parseInt
    更好的选择@CMS:好的观点+1进行深入分析。我没有注意到具有相同id的多个输入元素。捕捉得好。似乎OP需要对货币金额求和,
    parseFloat
    或一元加号运算符比
    parseInt
    @CMS:Good point更好+1进行深入分析。我没有注意到多个输入元素具有相同的id。很好。