テーブル セルの合計値に NaN の結果が表示されます。ユーザーが数量を入力するたびに計算される 2 つの合計セルを追加しています。なぜこの結果が得られたのかを理解しようとしています。これは、計算を含む関数のコードです。
<html>
<head>
<title> Cart </title>
<h1> My cart </h1>
<script type = "text/javascript">
function retr()
{
var cke = document.cookie;
var tot1 = document.getElementById("t1").value;
var tot2 = document.getElementById("t2").value;
if (cke.length>0) {
start = cke.indexOf("Circle=");
if (start!= -1) {
start = start + 7
end = cke.indexOf("$", start);
if (end == -1) end = cke.length;
qu1 = cke.substring(start, end);
document.getElementById("q1").value = qu1;
}
}
if (cke.length>0) {
start1 = cke.indexOf("$");
if (start1!= -1) {
start1 = start1 + 0
end = cke.indexOf("e", start1);
if (end == -1) end = cke.length;
pr1 = cke.substring(start1, end);
document.getElementById("p1").value = pr1;
}
}
document.getElementById("t1").value = parseFloat(qu1) * parseFloat(pr1);
if (cke.length>0) {
start = cke.indexOf("Stickman=");
if (start!= -1) {
start = start + 9
end = cke.indexOf("$", start);
if (end == -1) end = cke.length;
qu2 = cke.substring(start, end);
document.getElementById("q2").value = qu2;
}
}
if (cke.length>0) {
start = cke.indexOf("$");
if (start!= -1) {
start = start + 54
end = cke.indexOf("e", start);
if (end == -1) end = cke.length;
pr2 = cke.substring(start, end);
document.getElementById("p2").value = pr2;
}
}
alert(qu1);
alert(pr1);
alert(qu2);
alert(pr2);
alert(t2);
document.getElementById("t2").value = parseFloat(qu2) * parseFloat(pr2);
document.getElementById("tot").value = parseFloat(tot1) + parseFloat(tot2);
document.getElementById("ret").value = cke;
}
</script>
</head>
<body onload = "retr()">
<table border = "1">
<td>Circle </td>
<td><input type = "text" size = "8" id = "q1" readonly = "readonly" /></td>
<td> <input type = "text" size = "8" id = "p1" readonly = "readonly" /> </td>
<td> <input type = "text" size = "8" id ="t1"> </td>
<tr> </tr>
<td> Stickman </td>
<td> <input type = "text" size = "8" id = "q2" readonly = "readonly" /></td>
<td> <input type = "text" size = "8" id = "p2" readonly = "readonly" /> </td>
<td> <input type = "text" size = "8" id = "t2" > </td>
<tr> </tr>
<td colspan = "3"> TOTAL:</td>
<td> <input type = "text" size = "8" id = "tot"> </td>
</table>
<br /> <br />
<input type ="text" id = "ret" readonly = "readonly" />
<br / > <br />
<input type = "button" value = "Checkout">
<br /> <br />
<a href = "store.html" > Continue Shopping </a>
</body>
</html>