不同性别的基础代谢率应该有不同的值。我的计算器显示男性的结果是选择男性还是女性单选按钮。
男性BMR计算BMR = 66.5 +(13.75×体重kg)+(5.003×身高cm) - (6.755×年龄) 女性BMR = 655.1 +(9.563×体重kg)+(1.850×身高cm) - (4.676×年龄)
<!DOCTYPE html>
<html>
<head>
<script>
var bmr;
function funcCalc() {
var age = document.getElementById("age").value;
var gender = document.getElementById("gender").value;
var height = document.getElementById("height").value;
var weigth = document.getElementById("weigth").value;
if (gender == "masc") {
bmr = 66.5 + ( 13.75 * weigth ) + ( 5.003 * height ) - ( 6.755 * age )
} else {
bmr = 655.1 + ( 9.563 * weigth ) + ( 1.850 * height ) - ( 4.676 * age )
}
document.getElementById("lblResult").innerHTML = bmr;
}
</script>
</head>
<body>
<form action="#">
<input type="radio" name="gender" id="gender" value="masc" checked> Male<br>
<input type="radio" name="gender" id="gender" value="fem"> Female<br>
Age:<br>
<input type="number" id="age" value="20"><br>
Height:<br>
<input type="number" id="height" value="180"><br>
Weight:<br>
<input type="number" id="weigth" value="80"><br>
</form>
<button type="button"
onclick="funcCalc()">
Result</button>
<p id="lblResult">BMR</p>
</body>
</html>
答案 0 :(得分:0)
id
属性的值在每个文档上必须是唯一的。所以你的HTML无效。相反,您可以使用getElementsByName
来获取元素并检查每个元素的值。
let elements = document.getElementsByName('gender');
let isMale = elements[0].value === 'masc' || elements[1].value === 'masc';