2011-09-01 57 views
0

第一篇文章。我正在嘗試創建一個文本「輸入」框,該框允許我的網站中的任何三個命令「誰」,「爲什麼」和「如何」鏈接到相應的頁面。爲了澄清,我希望我的主頁有一個文本框,允許用戶鍵入上述三個命令之一,然後按「回車」鏈接到相應的頁面。聽起來很容易,但我是一個超級初學者。任何幫助將非常感謝。通過文本框中的特定輸入文本進行導航?

回答

0

您可以在JavaScript中的輸入框中添加一個keydown監聽器,並偵聽鍵碼13(輸入按鍵密碼)。如果它被按下,測試誰的價值,原因和方式。

<input type="text" id="LinkBox" /> 

document.getElementById("LinkBox").onkeydown = function(e) { 
    var keyCode = e.keyCode || e.which; 
    // 13 is keycode for the enter key 
    if (keyCode === 13) { 
     if (this.value === "who") { 
      window.location = // who URL here 
     } else if (this.value === "why") { 
      window.location = // why URL here 
     } else if (this.value === "how") { 
      window.location = // how URL here 
     } 
    } 
} 
+0

哇,非常感謝你,這個網站是不可思議的。你的方式好噶。 – TheRodStu