我有一個遊戲,你可以在每次回合中輸入你的分數到輸入框中。我寫了一個for循環來添加每個回合的總數,並將它顯示在「Total Score」框中。然而,在本場比賽中,如果在所有回合結束時你的「總得分」大於或等於50,你會得到10分的獎勵。我需要幫助編寫這個IF語句在JS
所以我創建了另一個名爲「Total」的輸入框。我想創建一個IF語句,以便如果「總分」輸入框中的數字大於或等於50,則您的分數加上10分獎金將顯示在其下方的「總計」框中。我怎樣才能做到這一點?
這裏是我的代碼:
//input form
Turn 1: <input onblur="findTotal()" type="text" name="qty">
<br>
Turn 2: <input onblur="findTotal()" type="text" name="qty">
<br>
Trun 3: <input onblur="findTotal()" type="text" name="qty">
<br>
<b>Total Score:</b> <input type="text" name="TotalScore" id="TotalScore">
<br>
Bonus (+10): <input type="text" name="bonus">
<br>
<b>Total:</b> <input type="text" name="Total">
//for loop
function findTotal() {
var arr = document.getElementsByName('qty');
var tot = 0;
for (var i = 0; i < arr.length; i++) {
if (parseInt(arr[i].value))
tot += parseInt(arr[i].value);
}
document.getElementById('Total').value = tot;
}
onblur = "findTotal()"
//my attempt at the IF statement ("alert hello" is just a place holder)
var tots = document.getElementById('TotalScore');
if (tots.value >= 50) {
alert("hello");
}
您的findTotal函數將運行一次外部的代碼,當頁面加載 - 也許你要移動的功能,裏面的代碼? –