這裏是我的代碼:在JavaScript中定義變量並創建按鈕?
<input id="numb" type="number" align="center">
<button type="button" onclick="myFunction()" align="center">Submit</button>
<p id="demo"></p>
<script>
function myFunction() {
var x, text;
// Get the value of the input field with id="numb"
x = document.getElementById("numb").value;
if (x == 1) {
text = "Fantasy World";
}
if (x == 2) {
text = "Sir Wags A Lot";
}
if (x == 3) {
text = "Take a Path";
}
if (x == 4) {
text = "River Clean Up"
}
if (x == 5) {
text = "Pinball"
}
if (x == 6) {
text = "Ghost Girl"
}
if (x == 7) {
text = "Dress Up"
}
if (x == 8) {
text = "Where Is My Hat?"
}
if (isNaN(x) || x < 1 || x > 8) {
text = "Input not valid";
}
document.getElementById("demo").innerHTML = text;
}
</script>
我需要讓我的代碼給用戶一個按鈕,輸入一個有效的數字到輸入框後確認他們的訂單。
例如:
if (x == 1) {
text = "Example"
}
然後再從那裏再進行一次按鈕就可以點擊確認。然後他們應該顯示一些文字說「確認訂單」 - 任何人都可以幫助我嗎? :)
@傑米卡爾弗除了你的問題,你應該使用'如果其他'。 – ozil
@Jamie你可以使用散列而不是寫太多'if'語句 –
我知道,但是我遇到了Syntax問題,所以這使得它更容易。 –