簡體   English   中英

用JavaScript計算總數

[英]Calculating the total in 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">
<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>

您可能正在將字符串串聯在一起,因此從1 + 1獲得11 嘗試使用parseInt()將字符串轉換為數字,如下所示:

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

更新:正如CMS在另一個答案中指出的那樣,您還將生成具有相同ID的多個元素,這是無效的,並且除了getElementById()之外,該元素將僅返回一個元素。 請查看CMS的答案 ,以找到解決此問題的一種方法。

另外,請注意,如果將十進制數字加在一起,則不能如上所述使用parseInt() 您可以使用parseFloat()或一元+運算符將字符串轉換為數字。 但是,您必須謹慎使用浮點算法

除了其他人已經解決的類型轉換問題之外,通過查看您的代碼,我看到了一些問題:

  1. 通過在循環中創建多個具有相同id( id="ad" )的input元素,您正在生成無效的標記。
  2. document.getElementById僅返回一個元素。

我建議您放置一個class名,以標識要求和的input元素:

...
<td>
  <input class="amount" type="text" value="<?php echo $Amount;?>" />
</td>
...

然后,您將可以執行以下操作:

function getTotal() {
  var inputs = document.getElementsByTagName('input'),
      el, total = 0;

  for (var i = 0, n = inputs.length; i < n; i++) {
    el = inputs[i];
    if (el.className == "amount") {
      total += +el.value; // using the unary plus operator to convert to Number
    }
  }

  document.getElementById('s').value = total;
}

在此處查看示例。

我看到你有:

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

確保將num作為數值回顯。

如果那不起作用,並且您看到echoed值顯然是一個數字,請嘗試使用以下方式強制使用它:

var q = document.getElementById('ad').value * 1;

您不能使用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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM