[英]Why is this simple javascript program not working?
The alert window gives me the message NaN what is wrong? 警报窗口给我留言NaN出了什么问题? Thanx for all the help 谢谢所有的帮助
var weight = parseFloat(document.BMI.weight.value)
var height = parseFloat(document.BMI.height.value)
function calcBMI() {
var answer = weight * 703 / (height*height)
return answer;
}
</script>
<form action="" name="BMI">
<input type="text" name="weight" />
<input type="text" name="height" />
<input type="submit" onclick="alert(calcBMI())" />
</form>
<script type="text/javascript">
function calcBMI() {
var weight = parseFloat(document.BMI.weight.value)
var height = parseFloat(document.BMI.height.value)
var answer = weight * 703 / (height*height)
return answer;
}
</script>
<form action="" name="BMI">
<input type="text" name="weight" />
<input type="text" name="height" />
<input type="submit" onclick="alert(calcBMI())" />
</form>
Hopefully it works now :) 希望它现在有效:)
You need to move your variables into your function call. 您需要将变量移动到函数调用中。 *Edit.. I was beaten to the punch. *编辑..我被击败了。
<form action="" name="BMI">
<input type="text" name="weight" />
<input type="text" name="height" />
<input type="submit" onclick="alert(calcBMI())" />
</form>
<script type="text/javascript">
function calcBMI() {
var weight = parseFloat(document.BMI.weight.value)
var height = parseFloat(document.BMI.height.value)
var answer = weight * 703 / (height*height)
console.log(weight);
return answer;
}
</script>
function calcBMI() {
var weight = parseFloat(document.BMI.weight.value)
var height = parseFloat(document.BMI.height.value)
var answer = weight * 703 / (height*height)
return answer;
}
</script>
There is a problem with local and grobal variable. 本地和grobal变量存在问题。 While you are clicking the button the function is not fetching the value from the textboxes 当您单击该按钮时,该功能不会从文本框中获取值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.