简体   繁体   English

在 JavaScript 中单击按钮将数字添加到递增列表不起作用

[英]Add number to incrementing list on button click in JavaScript not working

Ok so in my code I'm supposed to enter a number between 4 and 15. Then I click a button to add it, Once that happens the number will be added to the total number of ages and to one of the leagues I have created (it should be incremented by 1, for example, if I enter a 12 it will be 1 if I enter 12 again the total will say 2).好的,所以在我的代码中我应该输入一个 4 到 15 之间的数字。然后我单击一个按钮来添加它,一旦发生这种情况,该数字将被添加到年龄总数和我创建的一个联赛中(它应该增加 1,例如,如果我输入 12,它将是 1,如果我再次输入 12,总数将是 2)。 When I try and run the code it does nothing.当我尝试运行代码时,它什么也没做。

 <html> <head> <title>Soccer Manager</title> <script> function addAge() { // gets the input age var age = Number(document.getElementById("age").value); // clears the input field document.getElementById("age").value = ""; // checks if age is between 4 and 15 if(age >= 4 && age <= 15){ // table cell to display total number of children var total = document.getElementById("total"); // table cell to display total number of junior var junior = document.getElementById("junior"); // table cell to display total number of intermediate var intermediate = document.getElementById("intermediate"); // table cell to display total number of senior var senior = document.getElementById("senior"); // increase total number of children by 1 total.innerHTML = Number(total.innerHTML) + 1; // if its a junior age if(age<=7) // increase total number of junior by 1 junior.innerHTML = Number(junior.innerHTML) + 1; // if its a intermediate age else if(age<=11) // increase total number of intermediate by 1 intermediate.innerHTML = Number(intermediate.innerHTML) + 1; // else it will be a senior age else // increase total number of senior by 1 senior.innerHTML = Number(senior.innerHTML) + 1; } // if age is not between 4 and 15 alert an error else{ window.alert("Age should be between 4 and 15"); } } } </script> </head> <body> <center> <h1> Soccer Manager </h1> <div> <table> <tr> <th>Total Children</th> <td id="total">0</td> </tr> <tr> <th>Junior</th> <td id="junior">0</td> </tr> <tr> <th>Intermediate</th> <td id="intermediate">0</td> </tr> <tr> <th>Senior</th> <td id="senior">0</td> </tr> </table> <input type="number" placeholder="Age" id="age"> <button onClick="addAge()">Add</button> </div> </center> </body> </html>

It looks like you forgot to add curly braces ( } ) in your statements.您似乎忘记在语句中添加花括号 ( } )。


To fix it, you need to add curly braces in the following locations.要修复它,您需要在以下位置添加花括号。

if (age >= 4 && age <= 15) {
  // Code...
} // Add me!

if (age <= 7) { // Add here!
  // Code...
} // Add here!

else if (age <= 11) { // Add here!
  // Code...
} // Add here!

Remember to add curly braces to make your JavaScript code work correctly!请记住添加花括号以使您的 JavaScript 代码正常工作!

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

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