将表单输入值作为路径附加到操作url [英] Appending form input value to action url as path

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

问题描述

我有这样的表单:

 < form action =http:// localhost / test> 
< input type =textname =keywords>
< input type =submitvalue =搜索>
< / form>

如果我输入一个值,在文本输入框中输入'hello'并提交表单,该URL看起来像: http:// localhost / test /?keywords = hello



我希望将值附加到操作路径。所以基本上,在表单提交之后,URL应该如下所示:

  http:// localhost / test / hello 


解决方案

您可以使用 onsubmit

 < form id =your_formonsubmit =yourFunction( )> 
< input type =textname =keywords>
< input type =submitvalue =搜索>
< / form>

函数yourFunction(){
var action_src =http:// localhost / test /+ document.getElementsByName(keywords)[0] .value;
var your_form = document.getElementById('your_form');
your_form.action = action_src;
}


I have a form like this:

<form action="http://localhost/test">
    <input type="text" name="keywords">
    <input type="submit" value="Search">
</form>

If I type a value, let's say: 'hello' in the text input field and submit the form, the URL looks like: http://localhost/test/?keywords=hello.

I want the value to get appended to the action path. So basically, after the form submission the URL should look like:

http://localhost/test/hello

解决方案

You can use onsubmit attribute and set the action inside a function for example:

<form id = "your_form" onsubmit="yourFunction()">
    <input type="text" name="keywords">
    <input type="submit" value="Search">
</form>

function yourFunction(){
    var action_src = "http://localhost/test/" + document.getElementsByName("keywords")[0].value;
    var your_form = document.getElementById('your_form');
    your_form.action = action_src ;
}

这篇关于将表单输入值作为路径附加到操作url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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