Html 表单值创建一个 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11811782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:05:37  来源:igfitidea点击:

Form value creates a URL

htmlformsurl

提问by andrew anderson

I am trying to create a very simple form with a little bit of extra code to get the results as described below: the problem is I don't know how to go about doing it.

我正在尝试使用一些额外的代码创建一个非常简单的表单,以获得如下所述的结果:问题是我不知道如何去做。

What I am trying to achieve:

我正在努力实现的目标:

I have a form which has one text input box with the name 'url'. I want the user to be able to input a number into the box. When the user submits the form they should be redirected to a new website. The new website's URL will be based on the number inputted into the form.

我有一个表单,其中有一个名为“url”的文本输入框。我希望用户能够在框中输入一个数字。当用户提交表单时,他们应该被重定向到一个新网站。新网站的 URL 将基于表单中输入的数字。

The first part of the URL will always be: http://name.com/

URL 的第一部分将始终是:http: //name.com/

Then the number that the user inputted will be attached to the end. So if 123456 is entered into the form then on submission of the form the user would be taken to http://name.com/123456

然后将用户输入的数字附加到末尾。因此,如果在表单中输入 123456,那么在提交表单时,用户将被带到http://name.com/123456

How can I get this working? I am guessing it will require JavaScript or something.

我怎样才能让它工作?我猜它需要 JavaScript 或其他东西。

回答by Hanky Panky

<script>
    function process()
    {
        var url = "http://name.com/" + document.getElementById("url").value;
        location.href = url;
        return false;
    }
</script>

<form onSubmit="return process();">
    URL: <input type="text" name="url" id="url">
    <input type="submit" value="go">
</form>

回答by jeremy

You can add onsubmit="this.action='http://google.com/'+this.fieldName.value;"to your tag.

您可以添加onsubmit="this.action='http://google.com/'+this.fieldName.value;"到您的标签。

回答by Chris Clower

This should do it:

这应该这样做:

<script type="text/javascript">
    function goToPage() {
        var page = document.getElementById('page').value;
        window.location = "http://name.com/" + page;
    }
</script>
<input type="text" id="page" />
<input type="submit" value="submit" onclick="goToPage();" />