0
我有一个php页面,我将能够减去从mysql数据库中提取一个值,另一个值由用户自己输入。我想究竟是同时进入第二个输入字段中的值输入的值应该从第一输入值从数据库调用被减去减去值应在另一输入... 代码是可见......如何减去输入值
<html>
<head>
<style>
td {font-size:13px;}
</style>
<script src="jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function(){
$('input').each(function() {
$(this).keyup(function(){
calculatedis($(this));
});
});
});
function calculatedis(src) {
var sub = 0;
var sumtable = src.closest('.sumtable');
sumtable.find('input').each(function() {
var amt1 = $('input[name="amt1"]').val();
var amt2 = $('input[name="amt2"]').val();
sub = (amt1 - amt2)
});
sumtable.find(".netAmt").val(sub.toFixed(2));
}
</script>
</head>
<body>
<form class="inward">
<?php
include("dbconnection.php");
$sql = "SELECT id,price FROM table1 WHERE id='3' ORDER BY id";
$res = mysql_query($sql) or die(mysql_error());
echo "<table border='1' align='center' cellspacing='0' cellpadding='3' class='sumtable'>";
echo "<tr bgcolor='#CCCCCC' height='30'>";
echo "<td>Price</td>";
echo "<td>Discounted Amt</td>";
echo "<td>Net Amt</td>";
echo "</tr>";
while($row2=mysql_fetch_array($sql2))
{
//change row color alternately
$i++;
$color = ($i%2) ? "#f8f8ff" : "#f0fff0";
echo "<tr bgcolor=" . $color . " onmouseover='ChangeBackgroundColor(this)' onmouseout='RestoreBackgroundColor(this)'>
$price = $row2["price"];
if (strlen(trim($price)) == 0)
{
echo "<td> </td>";
}
else
{
echo "<td valign='top'><input name='amt1' value=" . $price . " size='5'/></td>";
}
echo "<td valign='top' ><input name='amt2' size='5'/></td>";
echo "<td valign='top'><input name='netamt' class='netAmt' size='5'/></td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";
echo "<tr><td></td></tr>";
echo "<center>";
echo "<input type='submit' name='submit' value='Save' style='cursor:pointer;cursor:hand;'> ";
echo "<input type='reset' name='clear' value='Clear' style='cursor:pointer;cursor:hand;'>";
echo "</center>";
?>
</form>
</body>
</html>
不知道这有什么不对,这种帮助将不胜感激............
除此之外.....我可以得到这些减去的总和循环结束时的值如Total:
@Andrian感谢名单FR乌尔样的帮助,我试图高压部份了,但NaN被在减去值应该来... –
有价值观被添加在两个文本框中输入netAmt显示? NaN不是一个数字;空白字符串上的parseInt()将返回此值,并将NaN添加到另一个数字中。 –
哎呀....对不起,我忘了包括;在$(this).find('。netAmt')。val(sub)可能就是这样,为什么我得到NaN,但是谢谢你的朋友bcoz我的脚本循环建议我的脚本完美运行...... –