[英]How to calculate user input and show on the same page (javascript)
I want to code a BMI calculator, i'm new to javascript, so its a little task for me. 我想编写一个BMI计算器,我是javascript新手,所以对我来说是个小任务。 I have the HTML code ready and i linked the js file.
我已经准备好HTML代码,并链接了js文件。
In the javascript file i declared 2 variables that take the users input. 在javascript文件中,我声明了2个接受用户输入的变量。 Then i declared the result in a variable.
然后我将结果声明为变量。 After i made a function that displays that result on the page.
之后,我做了一个功能,可以在页面上显示该结果。 But i can't get it to work.
但是我无法使它工作。 I can only put in 1 id in the result variable.
我只能在结果变量中输入1个id。
How can i get this to work? 我该如何工作?
Thanks! 谢谢!
When i press on the button it only shows me the Weight
, and not the (Number(inputWeight)) / (Number(inputLength) * Number(inputLength))
; 当我按下按钮时,它只会显示我的
Weight
,而不显示(Number(inputWeight)) / (Number(inputLength) * Number(inputLength))
;
Thats the formula to calculate BMI. 多数民众赞成在计算BMI的公式。
<form name="myForm">
<div class="form-item">
<label class="form-label">Length (in cm):</label>
<input type="number" class="form-input" name="length" placeholder="Type your length" id="length">
</div>
<div class="form-item">
<label class="form-label">Weight (in kg):</label>
<input type="number" class="form-input" name="weight" placeholder="Type your weight" id="weight">
</div>
<div class="form-button">
<input class="button" id="submit-bmi" value="calculate" type="submit" onclick="displayBMI(); return false">
</div>
<p id="result" class="bmi-calculator__result js-bmiresult"></p>
</form>
The javascript: JavaScript:
var inputLength = document.getElementById('length');
var inputWeight = document.getElementById('weight');
var totalBMI = (Number(inputWeight)) / (Number(inputLength) * Number(inputLength));
function displayBMI() {
document.getElementById('result').innerHTML = "Your BMI is: " +
document.myForm.weight.value;
}
<script>
function displayBMI()
{
var length = Number(document.getElementById("length").value);
var weight = Number(document.getElementById("weight").value);
var result = weight/(length * length);
document.getElementById("result").innerHTML = result ;
}
</script>
You just insert this script on your code. 您只需将此脚本插入代码中即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.