【发布时间】:2018-06-23 17:36:07
【问题描述】:
我有一个简单的 Google 图片搜索表单,可在新窗口中打开。当我想将表单参数更改为 Unsplash(在其 URL 搜索中不使用查询字符串)时,表单会继续发送查询字符串;(
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="unsplash();"/>
<label for="unsplash">Unsplash</label>
<form id="form" method="GET" action="https://www.google.com/search?q=">
<input id="input" type="text" name="q" value="" required>
<input 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"),
helper = document.getElementById("helper");
function googleImages() {
form.action="https://www.google.com/search?q=";
input.name="q";
helper.name="tbm";
helper.value="isch";
}
function unsplash() {
form.action="https://unsplash.com/search/photos/";
input.name="";
helper.name="";
helper.value="";
}
如何创建一个从输出 URL 中删除查询字符串的函数? (并在单选选项需要时再次设置参数)
【问题讨论】:
标签: javascript forms query-string