为什么在键盘上按下“Enter”或“Return”键不能正常工作?
问题描述:
我正在尝试创建一个可以设置为我的浏览器主页的主页。 下面是一个“谷歌搜索”功能,只要你点击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>
如果您需要更具体的上下文,请告诉我,我将尽我所能提供。
非常感谢。
答
我只需要移除表格标签。
的工作原理如下:
<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>
为什么要使用JavaScript?只需将其设置为