IF语句,并链接到消息变量中的新html页面

IF statements in Javascript and linking to new html page in a message variable

本文关键字:html 页面 变量 消息 语句 链接 IF      更新时间:2023-09-26

你能帮我让javascript中的if语句正常工作吗?

此外,我已经在我的消息变量中添加了一个指向新页面的链接,但我认为这不太正确,对此的任何帮助也将不胜感激。

var messageLow = "Your results show that you currently have a low risk of developing diabetes. However, it is important that you maintain a healthy lifestyle in terms of diet and exercise.";
var messageMed = "Your results show that you currently have a medium risk of developing diabetes. For more information on your risk factors, and what to do about them, please visit our diabetes advice website at http://www.zha.org.zd/";
var messageHigh1 = "Your results show that you currently have a HIGH risk of developing diabetes. Your main risk factors are your age and your BMI. We advise that you contact the Health Authority to discuss your risk factors as soon as you can. Please fill in our contact form and a member of the Health Authority Diabetes Team will be in contact with you.";
                "Please click on this link to fill out a contact form:" <a href=contactForm.html>contact form</a>
var messageHigh2 =  "Your results show that you currently have a HIGH risk of developing diabetes. Your main risk factors are your age and your family history. We advise that you contact the Health Authority to discuss your risk factors as soon as you can. Please fill in our contact form and a member of the Health Authority Diabetes Team will be in contact with you.";
                "Please click on this link to fill out a contact form:" <a href=contactForm.html>contact form</a>
var messageHigh3 =  "Your results show that you currently have a HIGH risk of developing diabetes. Your main risk factors are your age and your sugar levels. We advise that you contact the Health Authority to discuss your risk factors as soon as you can. Please fill in our contact form and a member of the Health Authority Diabetes Team will be in contact with you.";
                "Please click on this link to fill out a contact form:" <a href=contactForm.html>contact form</a>
 var messageHigh4 =  "Your results show that you currently have a HIGH risk of developing diabetes. Your main risk factors are your BMI and family history. We advise that you contact the Health Authority to discuss your risk factors as soon as you can. Please fill in our contact form and a member of the Health Authority Diabetes Team will be in contact with you.";
                "Please click on this link to fill out a contact form:" <a href=contactForm.html>contact form</a>
var messageHigh5 =  "Your results show that you currently have a HIGH risk of developing diabetes. Your main risk factors are your BMI and your sugar levels. We advise that you contact the Health Authority to discuss your risk factors as soon as you can. Please fill in our contact form and a member of the Health Authority Diabetes Team will be in contact with you.";
                "Please click on this link to fill out a contact form:" <a href=contactForm.html>contact form</a>
var messageHigh6 =  "Your results show that you currently have a HIGH risk of developing diabetes. Your main risk factors are your family history and your sugar levels. We advise that you contact the Health Authority to discuss your risk factors as soon as you can. Please fill in our contact form and a member of the Health Authority Diabetes Team will be in contact with you.";
                "Please click on this link to fill out a contact form:" <a         href=contactForm.html>contact form</a>
 function displayMsg()
 {
    var age, bmi, family, sugar;
    age = document.querySelector('input[name = "age"]:checked').value;
    bmi = document.querySelector('input[name = "bmi"]:checked').value;
    family = document.querySelector('input[name = "family"]:checked').value;
    sugar = document.querySelector('input[name = "sugar"]:checked').value;
    var total = age + bmi + family + sugar;
    document.getElementById("container").innerHTML = "<b>Your result: </b><br>";
    if( total >= 0 && total <= 15)
    {
        document.getElementById("container").innerHTML += messageLow;
    }
    else if(total >= 16 && total <= 25)
    {
        document.getElementById("container").innerHTML += messageMed;
    }
    elseif (total > 25)
    {
        if (age=>10 && bmi =>10)
    }{
         document.getElementById("container").innerHTML += messageHigh1;
        }
    {
         if (age=>10 && family =>10)
     }{
        document.getElementById("container").innerHTML += messageHigh2;
        }
    {
        if (age=>10 && sugar =>10)
    }{
        document.getElementById("container").innerHTML += messageHigh3;
        }
    {
        if (bmi=>10 && family =>10)
    }{      
        document.getElementById("container").innerHTML += messageHigh4;
        }
    {
        if (bmi=>10 && sugar =>10)
    }{      
        document.getElementById("container").innerHTML += messageHigh5;
        }
    {   
        if (family=>10 && sugar =>10)
    }{      
        document.getElementById("container").innerHTML += messageHigh6;
        }
   else
    {
     break;
    }
}

这就是您的代码应该是什么样子的:

if( total >= 0 && total <= 15){
    document.getElementById("container").innerHTML += messageLow;
}else if(total >= 16 && total <= 25){
    document.getElementById("container").innerHTML += messageMed;
}else if(total > 25){
    if (age>=10 && bmi >=10){
      document.getElementById("container").innerHTML += messageHigh1;
    }else if(age>=10 && family >=10){
      document.getElementById("container").innerHTML += messageHigh2;
    }else if (age>=10 && sugar >=10) {
    document.getElementById("container").innerHTML += messageHigh3;
    }else if (bmi>=10 && family >=10){ 
    document.getElementById("container").innerHTML += messageHigh4;
    }else if (bmi>=10 && sugar >=10){  
    document.getElementById("container").innerHTML += messageHigh5;
    }else if (family>=10 && sugar >=10){ 
    document.getElementById("container").innerHTML += messageHigh6;
    }
}

请注意,您只对循环或switch语句使用break。您不需要在if语句后面加上";"。它被称为"条件",而不是参数。参数被传递给函数。并且您不将";"放在条件后面,或者在条件后面的代码("{}"中的代码)将始终执行,即使条件不满足;

是">="而不是"=>"

是"else if"不是"elseif"

希望这对您有所帮助:)