使用JavaScript代码从单选按钮获取数值

时间:2015-07-25 16:14:01

标签: javascript function

为什么此代码没有返回值。当我单击Calculate时,它不起作用。

2 个答案:

答案 0 :(得分:0)

在分配值时将==更改为=。

ageNumerical == 0;

ageNumerical = 0;

答案 1 :(得分:0)

比较是==

作业是=

您需要ageNumerical = 0;

查看此fiddle

以下是摘录。

function getValue() {

    var a = patientAge();
    var b = patientBmi();
    var c = history();
    var d = patientDiet();

    var totalValue;
    totalValue = a + b + c + d;

    document.getElementById('message').innerHTML = totalValue;
}

function patientAge() {
    var age = document.getElementsByName('ageRange');
    var ageBracket;
    var ageNumerical;

    for (var i = 0; i < age.length; i++) {
        if (age[i].checked) {
            ageBracket = age[i].value;
        }
    }

    if (ageBracket == "1-25") {
        ageNumerical = 0;
    } else if (ageBracket == "26-40") {
        ageNumerical = 5;
    } else if (ageBracket == "41-60") {
        ageNumerical = 8;
    } else {
        ageNumerical = 10;
    }
    return ageNumerical;
}

function patientBmi() {
    var bmi = document.getElementsByName('bmiRange');
    var bmiValue;
    var bmiNumerical;
    for (var i = 0; i < bmi.length; i++) {
        if (bmi[i].checked) {
            bmiValue = bmi[i].value;
        }
    }
    if (bmiValue == "0-25") {
        bmiNumerical = 0;
    } else if (bmiValue == "26-30") {
        bmiNumerical = 0;
    } else if (bmiValue == "31-35") {
        bmiNumerical = 9;
    } else {
        bmiNumerical = 10;
    }
    return bmiNumerical;

}

function history() {
    var patientHistory = document.getElementsByName('familyHistory');
    var historyAnswer;
    var historyNumerical;

    for (var i = 0; i < patientHistory.length; i++) {
        if (patientHistory[i].checked) {
            historyAnswer = patientHistory[i].value;

        }
    }
    if (historyAnswer == "No") {
        historyNumerical = 0;
    } else if (historyAnswer == "Grandparent") {
        historyNumerical = 7;
    } else if (historyAnswer == "Sibling") {
        historyNumerical = 15;
    } else {
        historyNumerical = 15;
    }

    return historyNumerical;

}


function patientDiet() {
    var dietQuestion = document.getElementsByName('dietHabits');
    var dietAnswer;
    var dietNumerical;
    for (var i = 0; i < dietQuestion.length; i++) {
        if (dietQuestion[i].checked) {
            dietAnswer = dietQuestion[i].value;
        }
    }

    if (dietAnswer == "Low sugar") {
        dietNumerical = 0;
    } else if (dietAnswer == "Normal sugar") {
        dietNumerical = 0;
    } else if (dietAnswer == "Quite high sugar") {
        dietNumerical = 7;
    } else {
        dietNumerical = 10;
    }

    return dietNumerical;

}
<div id="ageQuestion">
  <p>How old are you?</p>
  <label for="age1">1-25</label>
  <input type="radio" name="ageRange" value="1-25" />
  <label for="age2">26-40</label>
  <input type="radio" name="ageRange" value="26-40" />
  <label for="age3">41-60</label>
  <input type="radio" name="ageRange" value="41-60" />
  <label for="age4">60+</label>
  <input type="radio" name="ageRange" value="60+" />
</div>
<div id="bmi">
  <p>What is your BMI?</p>
  <label for="bmiLevel">0-25</label>
  <input type="radio" name="bmiRange" value="0-25" />
  <label for="bmiLevel">26-30</label>
  <input type="radio" name="bmiRange" value="26-30" />
  <label for="bmiLevel">31-35</label>
  <input type="radio" name="bmiRange" value="31-35" />
  <label for="bmiLevel">35+</label>
  <input type="radio" name="bmiRange" value="35+" />
</div>
<div id="family">
  <p>Does anybody in your family have Diabetes?</p>
  <label for="history">No</label>
  <input type="radio" name="familyHistory" value="No" />
  <label for="history">Grandparent</label>
  <input type="radio" name="familyHistory" value="Grandparent" />
  <label for="history">Sibling</label>
  <input type="radio" name="familyHistory" value="Sibling" />
  <label for="history">Parent</label>
  <input type="radio" name="familyHistory" value="Parent" />
</div>
<div id="diet">
  <p>How would you describe your diet?</p>
  <label for="diet">Low sugar</label>
  <input type="radio" name="dietHabits" value="Low sugar" />
  <label for="diet">Normal sugar</label>
  <input type="radio" name="dietHabits" value="Normal sugar" />
  <label for="diet">Quite high sugar</label>
  <input type="radio" name="dietHabits" value="Quite high sugar" />
  <label for="diet">High sugar</label>
  <input type="radio" name="dietHabits" value="High sugar" />
</div>
<button onclick="getValue()">Calculate</button>
<p id="message"></p>