我在我的表計算每個錶行值自動
成本有這樣的數據 郵資 利潤 貝寶 售價
2.75 16.75
5.75 19.75
3.99 17.99
在值花費表正從數據庫並在郵資值,利潤取,和paypal都是不變的。當我更新數據庫中的成本值時,它現在將顯示在表中,並根據我更新的數據進行自動計算,並在selling_price列中顯示答案。我對如何在jQuery中實現客戶端計算有點困惑。任何人都可以幫我解決這個問題。任何幫助將得到更多的讚賞。
這裏是我創建
<html>
<head>
<script language="javascript" src="jquery.js"></script>
<script>
$(document).ready(function() {
//iterate through each textboxes and add keyup handler to trigger sum event
$(".txt").each(function()
{
$(this).keyup(function() {
calculateSellingPrice();
});
});
});
function calculateSellingPrice() {
var sellingprice = 0;
//iterate through each textboxes and add the values
$(".txt").each(function() {
//add only if the value is number
if (!isNaN(this.value) && this.value.length != 0) {
sellingprice += parseFloat(this.value);
$(this).css("background-color", "#FEFFB0");
}
else if (this.value.length != 0){
$(this).css("background-color", "red");
}
});
$("#sellingprice").html(sellingprice.toFixed(2));
}
</script>
</head>
<body>
<table border = "0">
<tr bgcolor="#cccccc">
<td><center>Selling Price</center></td>
<td><center>Cost</center></td>
<td><center>Postage</center></td>
<td><center>Profit</center></td>
<td><center>Paypal</center></td>
</tr>
<?php
$result = mysql_query("SELECT cost FROM table");
while ($myrow = mysql_fetch_row($result))
{
?>
<tr>
<td>
<?php echo "<input type='text' id='sellingprice' name='sellingprice' size='10' />"; ?>
</td>
<td>
<?php echo "<input class='txt' type='text' id='cost' name='cost' size='10' value='$myrow[1]' />"; ?>
</td>
<td>
<?php echo "<input class='txt' type='text' id='postage' name='postage' size='10' value='3' />"; ?>
</td>
<td>
<?php echo "<input class='txt' type='text' id='profit' name='profit' size='10' value='6' />"; ?>
</td>
<td>
<?php echo "<input class='txt' type='text' id='paypal' name='paypal' size='10' value='5' />"; ?>
</td>
</tr>
<?php
}
?>
</table>
</body>
</html>
我剛剛找到在互聯網上的jquery代碼,雖然我瞭解一點jQuery代碼,但我的股票,無法繼續的代碼。
在格式化代碼和空格分隔的co時,請注意編輯器中的「{}」按鈕ntent。您不需要' '和'
'來完成此操作。另外,標記在哪裏? – 2011-12-18 03:57:28
需要多長時間才能完成所有這些' '? – 2011-12-18 04:00:56
這就像你要找的東西? http://jsfiddle.net/jxuvT/ – 2011-12-18 04:07:35