将表单输入值附加到操作 url 作为路径

Appending form input value to action url as path

我有这样的表格:

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

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

我希望将值附加到操作路径。所以基本上,在提交表单后 URL 应该是这样的:

http://localhost/test/hello

您可以使用 onsubmit 属性并在函数内设置操作,例如:

<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 ;
}
    

您可以使用 onsubmit 和使用 jQuery 获得相同的结果。

检查以下内容。

HTML代码:

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

jQuery代码:

    function yourFunction(){

    var action_src = $("keywords").val();
    var your_form = $('your_form').val();

    var urlLink = "http://localhost/test/";
    urlLink = urlLink + action_src;



    your_form.action = urlLink;

}