简体   繁体   English

我需要在JS中编写此IF语句的帮助

[英]I need help writing this IF statement in JS

I have a game where you enter your score into an input box for each turn you take. 我有一个游戏,您每转一圈,就将得​​分输入输入框。 I wrote a for loop to add the total of each turn and have it displayed in the "Total Score" box. 我编写了一个for循环来添加每个回合的总数,并将其显示在“总分”框中。 However in this game, if your "Total Score" at the end of all of your turns is greater than or equal to 50, you get a 10 point bonus. 但是,在此游戏中,如果您所有回合结束时的“总分”大于或等于50,您将获得10点奖励。

So I've created another input box called "Total". 因此,我创建了另一个名为“总计”的输入框。 I want to create an IF statement so that if the "Total Score" input box has a number in it that is greater than or equal to 50, your score plus the 10 point bonus is displayed in the "Total" box beneath it. 我想创建一个IF语句,以便在“总分”输入框中包含大于或等于50的数字时,您的分数加10点奖金将显示在其下的“总分”框中。 How can I do this? 我怎样才能做到这一点?

Here is my code: 这是我的代码:

//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");
}

You could try checking to see if the user got the bonus at the end of the findTotal function (where you anyways just finished calculating the total score ): 您可以尝试检查用户是否在findTotal函数的末尾获得了奖励(无论如何,您刚刚完成了total score计算):

 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('TotalScore').value = tot; var bonus = tot >= 50 ? 10 : ''; document.getElementById('bonus').value = bonus; document.getElementById('Total').value = tot + bonus; } 
 Turn 1: <input onblur="findTotal()" type="text" name="qty"> <br> Turn 2: <input onblur="findTotal()" type="text" name="qty"> <br> Turn 3: <input onblur="findTotal()" type="text" name="qty"> <br> <b>Total Score: </b> <input disabled="true" type="text" name="TotalScore" id="TotalScore"> <br> Bonus: <input disabled="true" type="text" id="bonus"> <br> <b>Total:</b> <input disabled="true" type="text" id="Total"> 

Provide the textbox id as, 提供文本框ID为,

<b>Total:</b> <input type="text" id="tots" name="Total">

and use if as, 并使用as

 if($('#tots').value>=50){}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM