javascript - 以“URL/value”形式提交搜索表单,不带查询字符串“?q=value”
问题描述
我有一个在新窗口中打开的简单图像搜索表单:
HTML
<input type="radio" id="google" name="image" onclick="googleImages();" checked/>
<label for="google">Google Images</label>
<input type="radio" id="unsplash" name="image" onclick="poly();"/>
<label for="unsplash">Poly</label>
<form id="form" method="GET" action="https://www.google.com/search?q=">
<input id="input" type="text" name="q" value="" required>
<input id="button" type="submit" value="Search" onclick="this.form.target='_blank';">
<input id="helper" type="hidden" name="tbm" value="isch">
</form>
JS
var
form = document.getElementById("form"),
input = document.getElementById("input"),
button = document.getElementById("button"),
helper = document.getElementById("helper");
function googleImages() {
form.action="https://www.google.com/search?q=";
input.name="q";
helper.name="tbm";
helper.value="isch";
}
function poly() {
form.action="https://poly.google.com/search/";
input.name="";
helper.name="";
helper.value="";
}
代码在这里工作
问题
当我在搜索表单中更改为 Poly 时,查询字符串继续设置“?” 在 URL 输出中,在搜索结果页面上生成错误。
Google 图片使用一些 URL 参数,例如google.com/search?q=lion&tbm=isch,我可以在表单中复制这些参数。但是,Poly 不使用查询字符串 (?) 或参数 (param=value),它们只使用“URL/值”,例如poly.google.com/search/lion
问题
如何创建一个函数,当我选择 Poly 查询字符串“?” 被删除在提交时生成输出“URL/值”?换句话说,将搜索词(值)作为 URL 的一部分传递,而不需要任何查询或参数。
解决方案
看起来你只是在导航到不同的 url。您不希望或不需要这是表单提交。而不是在 JavaScript 中提交表单调用 window.location.href=。
推荐阅读
- javascript - 如何使用插槽扩展 Vue 组件
- node.js - 如何防止 Gmail OAuth 2.0 Playground 令牌过期
- javascript - 我可以使用什么方法来等待所有子回调完成?
- android - 在使用 Android 10 的 pdf 应用程序上显示 pdf 文件时出现问题
- java - Docker Binding 使用 testcontainers 解析异常
- python-3.x - 将 for 循环抓取转换为池并行抓取?
- unity3d - 将 2 个着色器脚本放入 1 个着色器脚本(弯曲世界 + 卡通着色器)
- sql - 如何组合聚合函数 Sum() 和 Max()
- r - `[.data.frame`(x, c(key.ord, criteria)) 中的错误:选择了未定义的列
- c - 如何检查#line 指令中的文件名是否与文件的实际名称匹配?