搜索输入被发送到另一个页面上的另一个搜索栏

时间:2019-07-15 20:36:40

标签: javascript jquery html search

我正在尝试从主站点搜索栏中获取输入,以发送到第二个站点的搜索栏中,并显示第二个站点的搜索结果。

我尝试了一些几乎可以正常工作的代码,但是它没有搜索第二个站点,而是在URL中添加了搜索输入。

       <div class="form-group">
          <form id="search-products" method="get">
            <input type="search" class="form-control input-thick-border" id="test" placeholder="Search..." name="keyword">
          </form>

          <script type="text/javascript">
            document.getElementById('search-products').onsubmit = function() {
              window.location = "http://website.com/Search" + document.getElementById('test').value;
              return false;
                }
            </script>

        </div>

因此,如果我在第一个搜索栏中键入“ pen”,则它将链接到不存在的“ website / searchpen”,因此它将重定向到“未找到”页面。我感觉我非常接近,只是缺少一些从第一个搜索栏获取输入并在第二个搜索栏中搜索输入的代码。任何帮助或想法将不胜感激。

1 个答案:

答案 0 :(得分:1)

您实际上不需要JavaScript。普通表单提交完全可以满足您的需求。只需设置表单操作即可。

<form method="get" action="https://example.com/search">
  <input type="search" name="keyword" />
</form>

提交表单时,它将显示为:

https://example.com/search?keyword=Whatever+the+user+typed+in