单击“搜索”按钮将用户发送到新的选项卡,显示了另一个站点的结果?
我正在尝试在我的网站上创建一个搜索框。但是,当某人单击“搜索”或按“ Enter/return”时。我想要一个单独的选项卡以显示来自其他站点的搜索结果。
示例:
我的网站: www.example.com
# using another answer on stackoverflow
# I have tried a lot of different action urls.
# the website url to which I want to send the search:
<form method="get" action="website.com/?searchTerm">
<input type="text" name="searchTerm"/>
</form>
当我尝试使用上面的Enter并按ENTER进行演示时,我只需访问网站IE weblity.com/?searchterm
而不是结果页面。
我认为可能使用的另一种方法是,当 Enter> enter
或按钮
单击<<时,在网站的URL中替换 searchTerm
。代码> www.example.com 使用 a
标签。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 target属性在表单标签上。
不要忘记通知您的用户,包括屏幕阅读器用户,该操作将打开一个新选项卡。
如果使用GET方法,则输入名称和值将作为查询参数传递。因此,
&lt; input name =“搜索term” value =“ test”&gt;
将变成?搜索term = test
在请求的URL中。以上代码在交互式堆栈片段中无法使用,因为它将在iFrame内部。
如果您在目标网站
上检查搜索表格,您会发现他们使用
searchDisplay
作为操作。此外,它们还包括一堆以其形式的隐藏字段。您还需要弄清楚您的表格中还包含哪些,以使您的查询不会被阻止。不过,请当心这是目标商店的实现详细信息,而不是稳定搜索像OpenSearch这样的API 。因此,它可能随时改变,您的搜索表将不再起作用。
目标网站上的表单代码:
You can use the target attribute on the form tag.
Don’t forget to inform your users, including screen reader users, that the action will open a new tab.
If you use the GET method, input names and values are going to be passed as query parameters. So
<input name="searchTerm" value="test">
will become?searchTerm=test
in the requested URL.The above code would not work in an interactive stack snippet, because it would be inside an iframe.
For your search to actually work with the target site
If you inspect the search form on your target website, you’ll notice they use
SearchDisplay
as the action. Additionally, they include a bunch of hidden fields in their form. You will need to figure out which ones to include in your form as well, for your query to not get blocked.Beware, though, that this are implementation details of the target shop, not a stable search API like OpenSearch. So it might change at any time, and your search form will no longer work.
The form code from the target website: