Html 表单提交到_blank后如何刷新表单页面?

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

How can I refresh a form page after the form submits to _blank?

javascripthtmlformssubmitonsubmit

提问by Hal Carleton

I have an HTML form which targets _blank. I would like the page that form is on to reload after submitting.

我有一个 HTML 表单,它针对_blank. 我希望在提交后重新加载表单所在的页面。

So here's some sample markup:

所以这里有一些示例标记:

<form method="POST" action="result.php" target="_blank">
    <label for="name">Name:</label>
    <input type="text" name="name" />

    <label for="email">Email:</label>
    <input type="email" name="email" />

    <input type="submit" value="submit" />
</form>

So when you submit this form it will POSTto result.phpin a new window or tab. This leaves the form page as is. I want the form page to reload, or at least reset all fields.

因此,当您提交此表单时,它会POST出现result.php在新窗口或选项卡中。这使表单页面保持原样。我希望表单页面重新加载,或者至少重置所有字段。

I've tried <form onsubmit="location.reload()"...as well as onsubmit="window.location.reload()"but nothing changed.

我也试过<form onsubmit="location.reload()"...了,onsubmit="window.location.reload()"但没有任何改变。

Any suggestions?

有什么建议?

(please no jquery)

(请不要jquery)

回答by Explosion Pills

Not sure why window.location.reload()doesn't work. I think it should. However, this does seem to work:

不知道为什么window.location.reload()不起作用。我认为应该。但是,这似乎确实有效:

onsubmit="setTimeout(function () { window.location.reload(); }, 10)"

回答by cssyphus

There are two ways to do this, one that you may not want to hear about.

有两种方法可以做到这一点,一种您可能不想听到。

FIRST:

第一的:

In the form init tag, if you set action=""then the form will submit to the same page (to itself).

在表单 init 标记中,如果您设置,action=""则表单将提交到同一页面(向其自身)。

<form action="" method="post">

This allows you to add server-side code to the top of the page, like this (using PHP, for example):

这允许您将服务器端代码添加到页面顶部,如下所示(例如,使用 PHP):

<?php
    If (empty($_POST)===false) {
        //server side scripting to handle the submitted form
    }else{
?>
    //HTML to create/show the page with form for user input
<?php
    //Close the if statement
    }
?>

SECOND:

第二:

Use AJAX (javascript or jQuery) to "submit" the form, and then -- in the AJAX function's callback -- carry on with whatever changes you want to make to the page, including redirecting to a new page.

使用 AJAX(javascript 或 jQuery)“提交”表单,然后 - 在 AJAX 函数的回调中 - 继续对页面进行任何更改,包括重定向到新页面。

回答by cssyphus

The accepted answer did not produce the desired results for me.

接受的答案没有产生我想要的结果。

Accepted answer:
onsubmit="setTimeout(function () { window.location.reload(); }, 10)"

Changing the accepted answer from onsubmit to onclick did fix my issue;

将接受的答案从 onsubmit 更改为 onclick 确实解决了我的问题;

Worked for me:
onclick="setTimeout(function () { window.location.reload(); }, 3)"

There may be undesirable results, or this may not be preferred but it performs as desired for me.

可能会有不良结果,或者这可能不是首选,但它按我的需要执行。

回答by asa

location.refresh(true);

will help

会有所帮助