2014-10-19 78 views
-1

下面的代码无法按预期工作。我已经排除了其余的标签。每当我执行html时,警报都没有提醒。有没有人遇到过这个问题?Javascript未提示输入值

<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> 
+1

它看起来并不像你实际上是调用'计算()'函数... – Lix 2014-10-19 11:14:08

+0

你在哪里调用计算( )? – 2014-10-19 11:14:14

+0

顺便说一下,你的HTML也存在一些问题... – Lix 2014-10-19 11:14:49

回答

2

您没有正确获取元素 - input.earth意味着什么。您应该使用getElementById()

var weight = document.getElementById('earth').value; 
+0

在我以前的代码中,formid.elementid.value的格式已经完美工作 – Yourfavouritenoob 2014-10-19 11:18:38

+0

'

'的ID是否为'input'? – Scimonster 2014-10-19 11:19:47

+0

@Yourfavouritenoob没有#'有问题... – 2014-10-19 11:22:00

0

,你也可以尝试:

<input type="text" id ="earth"> Enter your weight on Earth 
<button onclick="alert(document.getElementById('earth').value)">alert</button> 
+0

添加上下文和解释为什么这是解决问题的办法 – 2014-10-19 12:14:59