将表单输入值发送到URL [英] Send form input value to URL

查看:102
本文介绍了将表单输入值发送到URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的搜索表单:

I have a simple search form:

<form method="get" action="">
<input id="search" placeholder="Search" type="search" name="search"></input>
</form>

我想从搜索中获取值并将其显示在网址中,如下所示:

I want to get the value from "search" and have it show up in the url like this:

http://domain.ca/blog/search/searchterm

因此,如果访问者搜索kubrick,他们会得到如下URL:

So if a visitor searches for "kubrick", they'll get a URL like:

http://domain.ca/blog/search/kubrick

我无法使用PHP。有没有一个Javascript解决方案?

I can't use PHP. Is there a Javascript solution?

推荐答案

您可以尝试JavaScript重定向onsubmit事件。
为避免空搜索,请使用属性required作为输入。

You may try a javascript redirection for onsubmit event. To avoid empty searches, use the attribute "required" for the input.

示例:

Example:

<form onsubmit="window.location = 'http://domain.ca/blog/search/' + search.value; return false;">
<input id="search" placeholder="Search" type="search" name="search"></input>
<input type="submit" value="Send">
</form>

这篇关于将表单输入值发送到URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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