我已经创建了一个应用程序来计算 BMR。这是我写下面到目前为止:你只需在 CM 中输入你的身高,并在 Kilograms 中输入体重。但是,当我将计算 BMR 时,按钮不起作用。
<tr>
<td cl="height">Your Height<strong></strong>
</td>
<td>
<input type="number" name="height" id="height" tabindex="7" value="height" min="50" >Centimeter
</td>
</tr>
<tr>
<td cl="weight">Your Weight<strong></strong></td>
<td>
<input type="number" id="weight" name="Weight" tabindex="13" value="weight" min="100" >Kilograms
</td>
</tr>
<p>
Select your activity level:
</p>
<td cl="activity">
<tr><td><input type="radio" name="activity" id="activity" tabindex="15" value="1.53"> Rarely or no exercise </td></tr>
<tr><td><input type="radio" name="activity" id="activity" tabindex="17" value="1.76"> Ligthly or 1 - 3 days per week </td> </tr>
<tr><td><input type="radio" name="activity" id="activity" tabindex="19" value="1.76"> Moderate or 3-5 days per week </td></tr>
<tr><td><input type="radio" name="activity" id="activity" tabindex="21" value="2.25"> Active or 6-7 days per week </td> </tr>
<tr><td><input type="radio" name="activity" id="activity" tabindex="23" value="2.25"> Super active or very hard exercise</td></tr>
</td>
<td>
<input type="on" value="Calculate_bmr" onclick="Calculate_bmr()";>
<on type="reset" onclick="clearErr()">Reset</on>
</td>
</table>
</form>
<script src="Assignment1.js"></script>
这是我的功能
函数 Calculate_bmr () {
var bmr;
var age = doent.getElementsByName("age").Value;
var gender = doent.getElementsByName("gender").value;
var height = doent.getElementsByName("height").value;
var weight = doent.getElementsByName("weight").value;
var activity = doent.getElementsByName("activity").value;
}
我知道这很可怕。我一整天都在努力,不知道哪里出错了。
开关 (活动) {
case "1.53":
if(gender = "male"){
bmr = (66.5 + (13.75 * weight) + (5.003 * height) + (6.755 * age)) * 1.53;
window.alert(bmr);
break;
} else {
bmr = (655 + (9.563*weight) + (1.850*height) + (4.676*age)) * 1.53;
break;
}
case "1.76":
if(gender = "male"){
bmr = (66.5 + (13.75*weight) + (5.003*height) + (6.755*age)) * 1.76;
window.alert(bmr);
break;
} else {
bmr = (655 + (9.563*weight) + (1.850*height) + (4.676*age)) * 1.76;
break;
}
case "2.25":
if(gender = "male"){
bmr = (66.5 + (13.75*weight) + (5.003*height) + (6.755*age)) * 2.25;
window.alert(bmr);
break;
} else {
bmr = (655 + (9.563*weight) + (1.850*height) + (4.676*age)) * 2.25;
break;
}
}
所以,为了让你得到的活动的价值,你需要在每个循环,并检查它的“检查”或不,并得到它的价值。
function Calculate_bmr(){
var bmr;
var elements_activity = doent.getElementsByName('activity');
var activity;
elements_activity.forEach(e => {
if (e.checked) {
//if radio on is checked, set the value
//you can get the value of activity by checking each loop of the elements activity
activity = e.value;
}
});
alert(activity);
}
<p>
Select your activity level:
</p>
<td cl="activity">
<tr><td><input type="radio" name="activity" id="activity" tabindex="15" value="1.53"> Rarely or no exercise </td></tr>
<tr><td><input type="radio" name="activity" id="activity" tabindex="17" value="1.76"> Ligthly or 1 - 3 days per week </td> </tr>
<tr><td><input type="radio" name="activity" id="activity" tabindex="19" value="1.76"> Moderate or 3-5 days per week </td></tr>
<tr><td><input type="radio" name="activity" id="activity" tabindex="21" value="2.25"> Active or 6-7 days per week </td> </tr>
<tr><td><input type="radio" name="activity" id="activity" tabindex="23" value="2.25"> Super active or very hard exercise</td></tr>
</td>
<td>
<input type="on" value="Calculate_bmr" onclick="Calculate_bmr()";>
<on type="reset" onclick="clearErr()">Reset</on>
</td>
</table>
在获得活动的值之后。请记住,身高,体重和活动都是字符串,您需要将其转换为数字,以便执行任何操作,例如
bmr = (66.5 + (13.75 * weight) + (5.003 * height) + (6.755 * age)) * 1.53;
但开关的情况下,你有要求一个字符串“1.53,1.76,2.25”,所以你需要转换的活动后,开关的情况:
因此,将字符串转换为数字:您可以使用 PInt 或 Number,只需在此处了解更多https://www.w3scols.com/jsref/jsref_pint.asp
本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处
评论列表(86条)