我試圖使用if ... else語句並通過數組元素的值將值分配給變量stateCode。有些東西可能無法正常工作。 下面的代碼:if ... else語句和數組
<!DOCTYPE html>
<html>
<head>
<title>if...else</title>
<script type="text/javascript">
function choices() {
var stateCode = document.getElementById("x").value;
var taxes = new Array(10; 7.5; 3.2);
if ("x" === "OR") {
document.writeln(taxes[0]);
} else if ("x" === "CA") {
document.writeln(taxes[1]);
} else if ("x" === "MO") {
document.writeln(taxes[2]);
}
}
</script>
</head>
<body>
<p>
<select id="x" onchange="choices(this.value);">
<option value = "">---Reset---</option>
<option value = "MO">MO</option>
<option value = "OR">OR</option>
<option value = "CA">CA</option>
</select>
</p>
</body>
</html>
我懷疑字符串' 「X」'會永遠等於字符串'「或」「。 .. – deceze 2012-04-24 02:21:49
也許你打算寫'if(stateCode ===「OR」)'&c。 – 2012-04-24 02:23:12
';'也是'new Array(10; 7.5; 3.2)'中的語法錯誤。這些應該是逗號,並且確實應該使用數組文字符號:'[10,7.5,3.2]'。 – 2012-04-24 02:24:16