提交后的 HTML 表单重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17880977/
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 11:32:35  来源:igfitidea点击:

HTML Form Redirect After Submit

htmlforms

提问by user2622661

This is my form code:

这是我的表单代码:

<form enctype="multipart/form-data" name="upload-file" method="post"  action="http://example.com/upload">
    <div class="formi">
        <input id="text-link-input" type="text" name="url" class="link_form" value="your link"  onfocus="if(this.value==this.defaultValue) this.value='';" onblur="if(this.value=='') this.value=this.defaultValue;"   />
        <input type="submit" value="OK" class="link_but" />
    </div>
    <div class="upl" title="Upload"><img src="http://example.com/example.png" alt="" style="vertical-align:middle;"/>Upload
        <input type="file" name="file" size="1" class="up" onchange = "document.getElementById('text-link-input').value = String(this.value).replace('C:\fakepath\','')"/>
    </div>
</form>

Now, I want to redirect the submitter to any page of my choice after the form data has been submitted, but the action resides on another website where I cannot edit. Is it possible to redirect the user to any page after he has submitted the data to that site?

现在,我想在提交表单数据后将提交者重定向到我选择的任何页面,但该操作驻留在我无法编辑的另一个网站上。在用户向该站点提交数据后,是否可以将用户重定向到任何页面?

From some Googling, this is what I have found. Adding this to form code:

从一些谷歌搜索,这是我发现的。将此添加到表单代码中:

onSubmit=window.location='http://google.com'

This didnt work. Maybe I didnt implement it correctly? This is what I did:

这没有用。也许我没有正确实现它?这就是我所做的:

<form enctype="multipart/form-data" name="upload-file" method="post" onSubmit=window.location='http://google.com' action="http://example.com/upload">

Another person says adding a hidden field should work:

另一个人说添加一个隐藏字段应该可以工作:

<input type="hidden" name="redirect" value="http://your.host/to/file.html"> 

How should I implement this is my code?

我应该如何实现这是我的代码?

Suggestions and help awaited...

正在等待建议和帮助...

回答by ggzone

Try this Javascript (jquery) code. Its an ajax request to an external URL. Use the callback function to fire any code:

试试这个 Javascript (jquery) 代码。它是对外部 URL 的 ajax 请求。使用回调函数触发任何代码:

<script type="text/javascript">
$(function() {
  $('form').submit(function(){
    $.post('http://example.com/upload', function() {
      window.location = 'http://google.com';
    });
    return false;
  });
});
</script>