2016-11-12 118 views
0

我正在嘗試創建一個可以設置爲我的瀏覽器主頁的主頁。 下面是一個「谷歌搜索」功能,只要你點擊Google按鈕,你就可以搜索你想搜索的任何東西。當我在文本區域按「輸入」時,它會重新加載我的頁面。爲什麼在鍵盤上按下「Enter」或「Return」鍵不能正常工作?

我的問題是,每當我按下鍵盤上的「輸入」,它只是重新加載頁面,而不是打開谷歌與我把任何文字放入框中。

我希望Google可以通過按回車鍵或Google按鈕來打開搜索框。

<form> 
    <input class="form-control radius-input" id="textbox" type="text" placeholder="Search on Google..." onkeydown="if (event.keyCode == 13 || event.which == 13) { location='http://www.google.com/search?q=' + encodeURIComponent(document.getElementById('textbox').value);}" /> 
    <a class="btn btn-common btn-lg pull-right " id="googleLink" href="notrequired" onclick="this.href='http://www.google.com/search?q=' + encodeURIComponent(document.getElementById('textbox').value);"> 
     <span>Google</span> 
    </a> 
</form> 

如果您需要更具體的上下文,請告訴我,我將盡我所能提供。

非常感謝。

+0

爲什麼要使用JavaScript?只需將其設置爲

'並將'name =「q」'添加到輸入字段即可。 – JJJ

回答

-1

我只需要移除表格標籤。

的工作原理如下:

<input class="form-control radius-input" id="textbox" type="text" placeholder="Search on Google..." onkeydown="if (event.keyCode == 13 || event.which == 13) { location='http://www.google.com/search?q=' + encodeURIComponent(document.getElementById('textbox').value);}" /> 
<a class="btn btn-common btn-lg pull-right " id="googleLink" href="notrequired" onclick="this.href='http://www.google.com/search?q=' + encodeURIComponent(document.getElementById('textbox').value);"> 
    <span>Google</span> 
</a> 
相關問題