【发布时间】:2018-09-14 21:08:18
【问题描述】:
当厘米被选择为选项时,我尝试将两个输入框更改为一个输入框到一个输入框到厘米。它适用于英尺和英寸,但当用户选择厘米时我只需要一个输入。如何使用 javascript 更改它?
<form action=" " onsubmit="">Enter your Height:
<input type="text" id="height"> </input>
Feet <input type="text" id="height2"> </input>Inches
<select id = "typeH">
<option value = "feet">Feet/Inches</option>
<option value = "cm">Centimeter</option>
</select>
这是我的 js 代码中的一个函数:
function calculateBMI() {
var weight = document.getElementById('weight').value;
var height = document.getElementById('height').value;
var height2 = document.getElementById('height2').value;
var typeW = document.getElementById("type").value;
var typeH = document.getElementById("typeH").value;
if (typeW === "lbs") {
weight = weight * 0.45;
} else if (typeW === "kg") {
weight = weight;
}
if (typeH === "feet") {
height = height * 0.3048;
height2 = height2 * 0.0254;
var totalheight = height + height2;
} else if (typeH === "cm") { //this is the part where i want to change the two input boxes to one if the user selects cm as type of height
typeH.addEventListner("click", change());
//change();
height = height * 0.0328084;
height2 = 0;
var totalheight = height + height2;
}
var total = weight / (totalheight * totalheight);
roundToTwo(total);
document.getElementById('result').value = roundToTwo(total);
}
【问题讨论】:
-
到目前为止你有什么尝试?
-
请贴出您目前编写的javascript代码以尝试解决问题。
-
在 option-element 上尝试 onclick 事件(以厘米为单位)
标签: javascript html