0

在我的代码中,我的整个函数运行一次意味着它们没有断点。程序这样运行,当我在第一个文本框中给出值时,它会在我制作的所有文本框中给我 0。请告诉我我在做什么。我的编码是:

<html>
<head>
<title>Puchase Recipt</title>
<script type="text/javascript">
function recipt()
{
var a = Number(document.f1.amount.value);
var b = Number(document.f1.quantity.value);
var pro = a*b;
if (pro > 100)
{
    var c = Number(document.f1.discount.value);
    var per = Number(pro * c / 100);
    var d = Number(document.f1.subtract.value);
    document.f1.subtract.value = per;
    }
var e = Number(document.f1.total.value);
var total = pro - per;
document.f1.total.value = total;
}
</script>
</head>
<body>
<h1>Purchase Recipt</h1>
<table border="1" bordercolor="#000099">
<form name="f1">
<tr>
<th>Amount per KG</th>
<td><input type="text" placeholder="Amount" name="amount" onChange="recipt()"/></td>
</tr>
<tr>
<th>Quntity of item</th>
<td><input type="text" placeholder="Quantity" name="quantity" onChange="recipt()"/></td>
</tr>
<tr>
<th>Discount in %</th>
<td><input type="text" placeholder="Discount" name="discount" onChange="recipt()"/></td>
</tr>
<tr>
<th>Deduct Amount</th>
<td><input type="text" placeholder="Deduct Amount" name="subtract" onChange="recipt()"/></td>
</tr>
<tr>
<th>Total Amount</th>
<td><input type="text" placeholder="Total Amount" name="total" onChange="recipt()"/></td>
</tr>
<th>Reset</th>
<td><input type="reset"/></td>
</tr>
</form>
</table>
</body>
</html>
4

2 回答 2

0

尝试在javascript中使用此代码..

function recipt()
{
var a = Number(document.f1.amount.value);
var b = Number(document.f1.quantity.value);
var pro = a*b;
var per=0;
if (pro > 100)
{
    var c = Number(document.f1.discount.value);
    per = Number(pro * c / 100);
    var d = Number(document.f1.subtract.value);
    document.f1.subtract.value = per;
    }
var e = Number(document.f1.total.value);
var total = pro - per;
document.f1.total.value = total;
}
于 2012-10-31T08:36:17.740 回答
0

你应该首先纠正的事情。

  1. 您不应该recipt在每个输入的更改事件上调用该函数。相反,您应该调用该文本框的更改事件,该事件预计将是最后一个被填充的事件,或者更好的方法是为其提供一个按钮并在按钮单击时调用该函数。

  2. 在进行任何计算之前,首先验证文本框是否为空。如果它们不为空,请检查文本框是否包含任何非数字数据,否则您的计算将失败。

尝试这样做,一些小工作将解决您的问题

于 2012-10-31T08:36:33.333 回答