これは私のJavaScriptコードです:
var strp = [0,19,26,33,40,46,49,57,61,65,67,73,76];
var i = document.getElementById("length").value;
function calculator(){
document.write("the total cost is " + strp[document.getElementById("length").value]);
document.write("total is ");
document.write(strp[i]);
}
ここに計算関数を書き込もうとしています。そしてhtml部分はこれです:
<form>
select size : <select id="length">
<option value="0">Please Choose One...</option>
<option value="1">10 inches</option>
<option value="2">12 inches</option>
</select>
<input type="button" value="Calculate" onclick="calculator()" />
</form>
このコードをブラウザで実行すると、「getelementbyid value」で配列にアクセスし、正しい答えが得られます。しかし、getelmentbyid 値を変数に格納し、変数を使用して配列にアクセスすると、出力が未定義になるのはなぜですか?