根据用户输入重定向到html页面

时间:2020-06-14 21:51:31

标签: javascript html

我在网页上有一个文本框,用户可以在其中输入城市的位置。我希望将其合并到URL中。我有一个处理这些URL的Spring控制器,但是首先我无法重定向到该URL。这是我的代码

<span id = "form">
            <form  id = "input" target="_self" action="http://localhost:8090/location/city" method="GET" th:action="@{/location/city}">
                <label for="loc" align = right>Location:</label>
                <input type="text" placeholder="Enter Location" th:name= "location" id="loc">
                <input type="submit" value="Submit" onclick="setMode()">
            </form>
            <script>
                document.getElementById("input").action = "http://localhost:8090/location/" + document.getElementById("loc").value;
            </script>

我只想添加城市的名称,但是当用户单击提交时,URL变成“ /?location = cityname”,我想要的是“ / location / cityname”。

1 个答案:

答案 0 :(得分:0)

<input type="submit" value="Submit" onclick="setMode()">

type =“ submit”将触发提交表单的基本浏览器标准。当您将其更改为不带href的锚链接时,它将起作用。您当然可以设置锚链接的样式,使其看起来像一个按钮。否则,您应该使用event.preventDefault();。以防止提交表单。

您可以使用;

<a onclick='setMode()' >Submit</a>

代替;

<input type="submit" value="Submit" onclick="setMode()">
相关问题