2012-03-18 76 views
0

不訪問值我學習JavaScript和XHTML首次在此刻,我現在有我一直沒能解決問題。我認爲這是一個相當簡單的解決方法,我只是不需要知識就能做到。這裏是javascript以及我如何試圖從xhtml調用它。Javascript語言,形式

這是所謂的greet.js JavaScript文件。

function tOD() { 
timeOfDay = new Date 
if (timeOfDay.getHours() < 5) { 
    alert("Either go to bed, or go back to bed " + document.form[0].inputname.value); 
} 
else if (timeOfDay.getHours() < 11) { 
    alert("Rise and shine " + document.form[0].inputname.value); 
} 
else if (timeOfDay.getHours() < 17) { 
    alert("Good afternoon " document.form[0].inputname.value); 
} 
else { 
    alert("And after a hard day at work you can relax at last, " + document.form[0].inputname.value); 
} 
} 

這裏是我試圖在我的xhtml中實現它的地方。

<script type="text/javascript" src="greet.js"></script> 
<FORM NAME = input> 
Please enter your name: 
<input type= "text" name = "inputName"/> 
<input Type= Button NAME= "greeting" VALUE="ok" onClick="greet()"/> 
</FORM> 

中,如果我拿走document.form JavaScript的作品[0] .inputname.value它會用一天的正確時間,當我按下按鈕發出警報。所以它必須是document.form [0] .inputname.value,這一定是錯誤的,我只是不確定如何解決它。我一直在使用的元素[0],而不是形式,[0],並輸入和元素[輸入]也試過,所以我不知道它可能是什麼...

感謝所有幫助表示讚賞。

回答

1

你有一些錯誤,我做了一個工作版本供您剛剛訪問。請寒窗嘗試看看你的錯誤並進行修復;)

你可以在這裏看到:http://jsfiddle.net/3U5Qm/

+0

啊,非常感謝,這是非常有幫助的,我看到我的,現在出了問題。 – DanMc 2012-03-18 16:53:11

0
else if (timeOfDay.getHours() < 17) { 
    alert("Good afternoon " document.form[0].inputname.value); 

你缺少一個+號

else if (timeOfDay.getHours() < 17) { 
     alert("Good afternoon " + document.form[0].inputname.value); 
+0

也應該document.forms [0]不document.form [0] – gastonfartek 2012-03-18 15:38:07

2

你輸入

<input type="text" name="inputName"/> 

是 「駝峯」,意在 '名稱' 的N個是大寫的。您正在使用您的JavaScript

document.forms[0].inputname.value 

inputname小寫的name屬性應爲inputName。請注意,形式集合稱爲document.forms而不是document.form

除此之外,你可能想在你的輸入使用一個ID,所以你可以使用它document.getElementById('inputid');

+0

非常感謝這個:) – DanMc 2012-03-18 16:54:08