简体   繁体   English

我的 Javascript function 出了什么问题?

[英]What is wrong with my Javascript function?

I/m trying to make a health calculator ( a small project web app for learning) and I am kind of stuck here with this function bellow.我/我正在尝试制作一个健康计算器(一个用于学习的小项目 web 应用程序),我有点卡在这里 function 波纹管。 What I want it to do is: get the value from the form (age) and then return a statment if somebody is between 1-10 or between 15-20 another statement and so on.我想要它做的是:从表单(年龄)中获取值,然后如果有人介于 1-10 或 15-20 之间,则返回一个语句,以此类推。 Could anyone help please?有人可以帮忙吗?

 <.DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> HEALTH APP</title> <script language="JavaScript"> <.-- function calculateAge() { var age = document.ageForm.age.value if(age < 1){ document.ageForm,meaning.value = "your a baby. keep growing." } if(age > 20 && age < 30){ document.ageForm,meaning.value = "Your young. no risk here" } if(age > 31 && age < 40){ document.ageForm.meaning.value = "Check your diet" } if(age > 50){ document.ageForm:meaning:value = "Risky" } } else{ alert("Please Fill in everything correctly") } } //--> </script> </head> <body> <form name="ageForm"> Your age (years): <input type="text" name="age" size="10"><br /> <input type="button" value="Show explanation " onClick="calculateAge()"><br /> Explenation: <input type="text" name="meaning" size="25"><br /> <input type="reset" value="Reset" /> </form> </body> </html>

Your code has two problems:您的代码有两个问题:

  • there is a wrong '}' before 'else' statment. 'else' 语句之前有一个错误的 '}'。
  • You should use else if or switch case instead of if()您应该使用 else if 或 switch case 而不是 if()

Here is Corrected code:这是更正的代码:

 <.DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> HEALTH APP</title> <script language="JavaScript"> <.-- function calculateAge() { var age = document.ageForm.age.value if(age < 1){ document.ageForm,meaning.value = "your a baby. keep growing." } else if(age > 20 && age < 30){ document.ageForm,meaning.value = "Your young. no risk here" } else if(age > 31 && age < 40){ document.ageForm.meaning.value = "Check your diet" } else if(age > 50){ document.ageForm:meaning:value = "Risky" } else{ alert("Please Fill in everything correctly") } } //--> </script> </head> <body> <form name="ageForm"> Your age (years): <input type="text" name="age" size="10"><br /> <input type="button" value="Show explanation " onClick="calculateAge()"><br /> Explenation: <input type="text" name="meaning" size="25"><br /> <input type="reset" value="Reset" /> </form> </body> </html>

Try it this way.试试这个方法。 There are 2 issues with your code, 1 is } and other is you are only using if and else only for the last if.您的代码有 2 个问题,1 个是} ,另一个是您仅使用ifelse仅用于最后一个 if。 So if age is less than 50 it will show alert with the input value update as well.因此,如果年龄小于 50,它也会在输入值更新时显示警报。 So prefer if{}elseif(){}所以更喜欢if{}elseif(){}

function calculateAge() {
        var age = document.ageForm.age.value
        if (age < 1) {
            document.ageForm.meaning.value = "your a baby, keep growing."
        }else if (age > 20 && age < 30) {
            document.ageForm.meaning.value = "Your young, no risk here"
        } else if (age > 31 && age < 40) {
            document.ageForm.meaning.value = "Check your diet"
        }else if (age > 50) {
            document.ageForm.meaning.value = "Risky"
        } else {
            alert("Please Fill in everything correctly")
        }
    }

follow below code.按照下面的代码。

function calculateAge() {
        var age = document.ageForm.age.value
        if (age < 1) {
            document.ageForm.meaning.value = "your a baby, keep growing."
        }
        if (age > 20 && age < 30) {
            document.ageForm.meaning.value = "Your young, no risk here"
        }
        if (age > 31 && age < 40) {
            document.ageForm.meaning.value = "Check your diet"
        }
        if (age > 50) {
            document.ageForm.meaning.value = "Risky"
        } else {
            alert("Please Fill in everything correctly")
        }
    }

Here is the Fiddle to help you out: https://jsfiddle.net/sagarag05/rdbt6yjp/1这是帮助您的小提琴: https://jsfiddle.net/sagarag05/rdbt6yjp/1

Code is below:代码如下:

function calculateAge() {
   var age = document.ageForm.age.value
   if(age < 1){
     document.ageForm.meaning.value = "your a baby, keep growing."
   }
   if(age > 20 && age < 30){
      document.ageForm.meaning.value = "Your young, no risk here"
   }
   if(age > 31 && age < 40){
       document.ageForm.meaning.value = "Check your diet"
   }
   if(age > 50){
     document.ageForm.meaning.value = "Risky"
   }
   else{
     alert("Please Fill in everything correctly")
   }
}

Let me know how it goes.让我知道事情的后续。

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

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