使用 HTML 表单创建 Google 搜索框

Luk*_*s_T 7 html forms google-search

因此,我尝试在我的网站上插入一个搜索表单,将用户的查询重定向到 Google 搜索。

我曾经能够使用以下方法来做到这一点:

<form role="search" method="get" id="searchform" class="searchform" action="http://www.google.co.uk/search?hl=en-GB&source=hp&q=">
  <div id="gform">
    <label class="screen-reader-text" for="s"></label>
    <input type="text" value="" name="s" id="s">
    <input type="submit" class="btn fa-input" value="&#xf002;" style="font-family: FontAwesome;">
  </div>
</form>
Run Code Online (Sandbox Code Playgroud)

然而,这不再有效。我猜这是因为 Google 更改了搜索 URL,但我无法使用 Google 当前的 URL 来复制此效果。

知道如何解决这个问题吗?

Luk*_*s_T 5

大家好,看来我自己已经解决了这个问题

<div id="gform">
  <label class="screen-reader-text" for="s"></label>
  <input type="text" value="" name="q" id="s">
  <input type="submit" class="btn fa-input" value="&#xf002;" style="font-family: FontAwesome;">	
</div>
Run Code Online (Sandbox Code Playgroud)
解决我的问题的关键是name="q"。这就是谷歌搜索工作所需的;基本上这是因为谷歌期望q作为名称


小智 5

这段代码创建了一个简单的谷歌搜索框

<form action="http://www.google.com/search" method="get">
    <input type="text" name="q"/>
    <input type="submit" value="search" />
</form>
Run Code Online (Sandbox Code Playgroud)