[英]Javascript not alerting input value
The code below isn't working as expected. 下面的代码无法正常工作。 I have excluded the rest of the tags. 我已经排除了其余的标签。 Whenever I execute the html, the alert alerts nothing at all. 每当我执行html时,警报都不会发出任何警报。 Has anyone encountered this problem before? 有人遇到过这个问题吗?
<p><input type="text" id = "earth"> Enter your weight on Earth.<BR><BR></p>
<script type="text/javascript">
function calculate()
{
var weight = input.earth.value;
window.alert(weight);
}
</script>
You're not getting the element correctly - input.earth
means nothing. 您没有正确获取元素input.earth
毫无意义。 You should use getElementById()
: 您应该使用getElementById()
:
var weight = document.getElementById('earth').value;
you may try also: 您也可以尝试:
<input type="text" id ="earth"> Enter your weight on Earth
<button onclick="alert(document.getElementById('earth').value)">alert</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.