【发布时间】:2018-04-23 18:14:32
【问题描述】:
我正在尝试将用户答案保存在一个变量中,然后在 if 语句中使用该变量。我尝试了以下代码,但它不起作用:
<input type="number" id="x"/>
<button onclick="calc();";>try</button>
<script>
function calc() {
var age = document.GetElementById("x").value;
if (age >= 35) {
alert("you are old enough");
} else {
alert("you are too young");
}
}
</script>
【问题讨论】:
-
GetElementById-->getElementById. -
GetElementById不存在。检查你的拼写。使用browser console (dev tools)(点击F12)并阅读任何错误。 -
第二个半列在:
<button onclick="calc();";>更改为:<button onclick="calc();"> -
在我的答案中添加了一个 sn-p。如果这对您有用,请接受:D。
标签: javascript html