gpt4 book ai didi

javascript - 带有两个附加字段的简单 BMI 计算

转载 作者:行者123 更新时间:2023-12-03 11:59:08 27 4
gpt4 key购买 nike

所以我有以下表格来计算用户的 BMI 并根据结果将用户重定向到特定页面:

HTML

<form name="bmiForm">
<p>Your Weight(kg): <input type="text" name="weight"></p>
<p>Your Height(cm): <input type="text" name="height"></p>

<input type="button" value="Calculate BMI" onClick="calculateBmi()"><br />

<p>Your BMI: <input type="text" name="bmi" size="10"></p>
</form>

JavaScript

<script>
function calculateBmi() {

var weight = document.bmiForm.weight.value
var height = document.bmiForm.height.value

if(weight > 0 && height > 0) {
var finalBmi = weight/(height/100*height/100)
document.bmiForm.bmi.value = finalBmi

if(finalBmi < 30){
window.location.href = 'http://localhost:8888/?page_id=146';
}

if(finalBmi > 30){
window.location.href = 'http://localhost:8888/?page_id=149';
}
}
}
</script>

这绝对是一种享受。但是,我现在还需要两个输入。

  1. 您尝试过节食和锻炼吗?
  2. 您曾经患有糖尿病吗?

因此,我显然需要为 if 语句中的每个功能和添加的功能选择"is"和“否”选项。

我尝试添加:

var diabetes = document.bmiForm.diabetes.value

到脚本,然后是 if 语句中的另一个条件,但没有运气。

从这里我可以去哪里?任何完成上述任务的建议。

任何帮助将不胜感激。

最佳答案

对于复选框,您必须这样做:

 if (document.getElementById('diabetes').checked) {    //here 'diabetes' is id of checkbox
alert("checked");
}
else { alert("not checked") }

对于下拉菜单,请执行以下操作:

var e = document.getElementById("diabetes");
var selectvalue= e.options[e.selectedIndex].value; //you can get selected value with this
var selecttext= e.options[e.selectedIndex].text; //you can get selected text with this

关于javascript - 带有两个附加字段的简单 BMI 计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25479407/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com