チェックボックスをオンにして小計をクリックすると、価格がテキストボックスに入力される JavaScript を使用して、Web プログラミングクラスの価格チェックボックスページを作成しています。私が抱えている問題はdocument.getElementById("subtotal").value = total
、テキストボックスに値が入力されていない if ステートメントを終了したときです。alert
orステートメントを挿入するdocument.write
と、それが入力されます。私が見逃しているものはありますか?これがアラートを配置した私のコードです(これは望ましくありません):
<!DOCTYPE html>
<head>
<title> Price Checkbox </title>
<meta charset = "utf-8" />
<script>
var total, a = 0;
</script>
<script type = "text/javascript">
function subtotal1()
{
if (document.getElementById('apple').checked)
{
total = a + .59;
a = total;
document.getElementById("subtotal").value = total
alert(total);
}
if(document.getElementById('orange').checked)
{
total = a + .49;
a = total;
document.getElementById("subtotal").value = total
alert(total);
}
if(document.getElementById('banana').checked)
{
total = a + .39;
a = total;
document.getElementById("subtotal").value = total
alert(total);
}
}
</script>
</head>
<body>
<form id = "myForm" action = "">
<p>
<label> <input type = "checkbox" id = "apple"/>
apple $.59 </label>
<br />
<label> <input type = "checkbox" id = "orange"/>
orange $.49 </label>
<br />
<label> <input type = "checkbox" id = "banana"/>
banana $.39 </label>
</p>
<button onclick = "subtotal1()">Submit</button>
<p></p>
<input type="textbox" id="subtotal"/>
</form>
</body>