--------------------------------------------------
|No|Style No|Item Desc|Qty|Unit Price|Total Price|
--------------------------------------------------
|1 |Style 1 |Item 1 |44 |3.00 |132.00 |
|2 |Style 2 |Item 2 |3 |3.00 |9.00 |
|3 |Style 3 |Item 3 |23 |34.00 |782.00 |
|4 |Style 4 |Item 4 |56 |78.00 |4368.00 |
|5 |Style 5 |Item 5 |34 |58.00 |1972.00 |
--------------------------------------------------
(Submit button)
GRAND TOTAL: RM[_________ textbox] (Calculate Total button)
或者,这是屏幕截图:http: //img715.imageshack.us/img715/8885/calcs.jpg
你好,我有一个表格,表格是根据数据库中的表格从 php 脚本生成的。我一直在努力研究如何在第一行之后制作以下行以使用 javascript 处理 onkeyup 事件。第一行(在红色方框中)作为上面的屏幕截图链接,似乎运行良好,例如,如果我将第一行中“Qty”的值更改为“1”,它将自动计算并产生“Total”的输出价格”到“3.00”,但不在后面的行中,每当我插入新值时它什么都不做。奇怪的是,当我单击“计算总计”按钮时,它确实计算了包括“总计”文本框中的每一行
我似乎无法弄清楚问题到底是什么以及如何解决它。任何帮助将不胜感激。
下面是源代码:
function calc(idx) {
var price = parseFloat(document.getElementById("cost"+idx).value)*
parseFloat(document.getElementById("qty"+idx).value);
// alert(idx+":"+price);
document.getElementById("price"+idx).value= isNaN(price)?"0.00":price.toFixed(2);
}
function totalIt() {
var qtys = document.getElementsByName("qty[]");
var total=0;
for (var i=1;i<=qtys.length;i++) {
calc(i);
var price = parseFloat(document.getElementById("price"+i).value);
total += isNaN(price)?0:price;
}
document.getElementById("total").value=isNaN(total)?"0.00":total.toFixed(2)
}
window.onload=function() {
document.getElementsByName("qty[]")[0].onkeyup=function() {calc(1)};
document.getElementsByName("cost[]")[0].onkeyup=function() {calc(1)};
}
</script>
</head>
<body>
<?php
$sql = "SELECT * FROM transaction_item";
$result = mysql_query($sql,$rfps) or die($sql."<br/><br/>".mysql_error());
//start a table
echo '<form name="form1" method="post" action="">
<table width="350px" border="1" style="border-collapse:collapse;">';
//start header of table
echo '<tr>
<th id="datatable" > <div align="center">No</div></th>
<th style="display:none;"><div align="center">Trans<br />Item<br />ID</div></th>
<th style="display:none;"><div align="center">Trans<br />Info<br />ID</div></th>
<th><div align="center">Style<br />No</div></th>
<th><div align="center">Item<br />Desc</div></th>
<th><div align="center">Qty</div></th>
<th><div align="center">Unit<br />Price</div></th>
<th formula="cost*qty"summary="sum"><div align="center">Total<br />Price</div></th>
</tr>';
//loop through all results
$i=1;
while($r=mysql_fetch_object($result)){
//print out table contents and add id into an array and email into an array
echo '<tr>
<td id="datatable"> <div align="center">'.$i.'</div></td>
<td style="display:none;"><div align="center">'.$r->trans_item_id.'</div></td>
<td style="display:none;"><div align="center">'.$r->trans_info_id.'</div></td>
<td><div align="center"><input type="text" id="style'.$i.'" name="style[]" value="'.$r->style_no.'" size="10" /></div></td>
<td><div align="center"><input type="text" id="item'.$i.'" name="item[]" value="'.$r->item_desc.'" size="25" /></div></td>
<td><div align="center"><input type="text" id="qty'.$i.'" name="qty[]" value="'.$r->qty.'" size="2" /></div></td>
<td><div align="center"><input type="text" id="cost'.$i.'" name="cost[]" value="'.$r->unit_price.'" size="5" /></div></td>
<td><div align="center"><input type="text" id="price'.$i.'" name="price[]" value="'.$r->total_price.'" size="6" /></div></td>
</tr>';
++$i;
}
//submit the form
echo'</table>
<input type="submit" name="Submit" value="Submit">
<p><strong>GRAND TOTAL: RM</strong>
<input type="text" readonly="readonly" id="total" />
<input type="button" value="Calculate Total" onclick="totalIt()" /></p>
</form>';
?>
</body>
</html>