0

私のコードでは、関数全体が1回実行されているということは、それらがブレークポイントではないことを意味します。プログラムはこのように実行され、最初のテキストボックスに値を入力すると、作成したすべてのテキストボックスに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 に答える