在我的代码中,我的整个函数运行一次意味着它们没有断点。程序运行就像这样,当我在第一个文本框中给出值时,它会在我生成的所有文本框中给出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>
答案 0 :(得分: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;
}
答案 1 :(得分:0)
你应该先纠正的事情。
您不应该在每次输入更改时调用recipt
函数
事件。相反,你应该调用该文本框的更改事件
预计将是最后一个被填补或更好的方式是给予
按钮,然后按下按钮调用该功能。
在做之前首先验证文本框是否为空 任何计算。如果它们不是空的,请检查文本框是否为空 不包含任何非数字数据,否则您的计算将 失败。
尝试这样做,一些小工作将解决您的问题