此脚本在 Safari 中产生预期结果,但在 Firefox 中没有。(这是我测试过的仅有的两个浏览器。)
这是相关的HTML:
<form name="inputForm">
Weight:<input type="number" name="weight"> (lbs) <br>
Height:<input type="number" name="height"> (inches) <br>
<hr>
<input type="button" name="process" value="Calculate" onclick="calcBMI(weight.value, height.value)">
<hr>
BMI: <input type="text" readonly name="bmiResult">
</form>
这是整个 Javascript,放置在<head>...</head>
HTML 的标签中:
<script>
var BMIMULTI = 703;
function calcBMI(weight, height)
{
var bmi = (weight * BMIMULTI) / (height * height);
document.inputForm.bmiResult.value = bmi;
}
</script>
两种浏览器中的控制台均未显示错误。我没有链接到 CSS 或 JS 的外部文件。
从我自己的调试中,我发现问题出height.value
在onclick="calcBMI(...)"
. 的值height.value
被传递给calcBMI
函数 as null
,而weight.value
被适当地传递。
在 Safari 中,这两个值都成功地传递到calcBMI
函数中。
为什么会这样?而且,为什么它会出现在 Firefox 而不是 Safari 中?
我是 javascript 新手,感谢您的帮助。