删除查询字符串“?"在HTML表单方法GET中 [英] Remove query string "?" in HTML form method GET

查看:75
本文介绍了删除查询字符串“?"在HTML表单方法GET中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Google图片搜索表单,该表单会在新窗口中打开.当我想将表单参数更改为Unsplash(在其URL搜索中不使用查询字符串)时,表单会继续发送查询字符串;(

I have a simple search form of Google Images that open in a new window. When I want to change the form parameters to Unsplash (that don't use query strings on their URL search) the form continue sending query string ;(

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中删除查询字符串的函数? (并在单选选项需要它们时再次设置参数)

How create a function that remove query string from output URL? (and set again parameters when a radio option need them)

推荐答案

因此,如果您不向unsplash.com发送任何参数,请不要使用表单提交.而是在unsplash()函数中使用javascript重定向.

So if you are not sending any parameters to unsplash.com don't use form submit. Instead use javascript redirect inside unsplash() function.

window.location.href = "https://unsplash.com/search/photos/";

这篇关于删除查询字符串“?"在HTML表单方法GET中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆