简体   繁体   English

将表单内容提交到URL

[英]Submit Form Contents to a URL

I'll be honest, I don't really know what the best approach here is. 老实说,我真的不知道最好的方法是什么。 I've got no Javascript knowledge, but I don't think should be necessary here...It's stupidly simple. 我没有Java语言知识,但我认为这里没有必要……这很简单。

I have a simple form. 我有一个简单的表格。 I want the user to be able to type a word and press enter or click "submit." 我希望用户能够输入单词,然后按Enter或单击“提交”。 When "X" is entered, I want them to be redirected to 'www.MyURL.com/X.html'. 输入“ X”后,我希望它们被重定向到“ www.MyURL.com/X.html”。 The only solution I could find looked like this: 我能找到的唯一解决方案是这样的:

<form>
<input name="solution" type="text" id="solution" maxlength="10" /><br />
<input type="button" value="submit" onclick="window.location='http://www.MYURL.com/' + this.form.solution.value + '.html'"/>
</form>

However, this doesn't allow the user to hit Enter to submit the form. 但是,这不允许用户点击Enter提交表单。 I tried the below to make it a submit input, but I don't know anything about the potential operations of "onsubmit", and this one isn't working. 我尝试了以下操作以使其成为提交输入,但是我对“ onsubmit”的潜在操作一无所知,并且此操作无效。

<form onsubmit="window.location='http://www.MYURL.com/' + this.form.solution.value + '.html'">
<input name="solution" type='text' id="solution" maxlength="10" /><br />
<input type="submit" value="submit" />

Should I be using "action=" for this event? 我应该为此活动使用“ action =“吗? And I don't know if "method=" plays into it. 而且我不知道“ method =“是否起作用。

My issue is that I can't figure out how to make the form submit its text content to a URL and then link to that URL. 我的问题是我无法弄清楚如何使表单将其文本内容提交到URL,然后链接到该URL。

You can do this all in javascript without a form. 您可以使用JavaScript而不用表格来完成所有这些操作。 Just check the key code with each keyboard press - if it's the Enter key (13), then do your redirect. 只需在每次按键盘时检查键代码-如果它是Enter键(13),然后进行重定向。

Here's a full working page: 这是完整的工作页面:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<input type="text" id="solution" onkeyup="doSomething(event);">
<input type="button" value="Click Me" onclick="redirect();">
<script type="text/javascript">
    function doSomething(e) {
        var keyCode = e.keyCode || e.charCode;
        if (keyCode === 13) {
            redirect();
        }
    }

    function redirect() {
        window.location.href = "http://www.MYURL.com/"
            + document.getElementById("solution").value
            + ".html";
    }
</script>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM