[英]How can I refresh a form page after the form submits to _blank?
我有一個針對_blank
的 HTML 表單。 我希望表單所在的頁面在提交后重新加載。
所以這里有一些示例標記:
<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>
因此,當您提交此表單時,它將在新的 window 或選項卡中POST
到result.php
。 這使表單頁面保持原樣。 我希望重新加載表單頁面,或者至少重置所有字段。
我試過<form onsubmit="location.reload()"...
以及onsubmit="window.location.reload()"
但沒有任何改變。
有什么建議么?
(請不要jquery)
不確定為什么window.location.reload()
不起作用。 我認為應該。 但是,這似乎有效:
onsubmit="setTimeout(function () { window.location.reload(); }, 10)"
有兩種方法可以做到這一點,一種你可能不想聽到的方法。
第一:
在表單init標簽中,如果設置action=""
則表單將提交到同一頁面(自身)。
<form action="" method="post">
這允許您將服務器端代碼添加到頁面頂部,例如(使用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
}
?>
第二:
使用AJAX(javascript或jQuery)“提交”表單,然后 - 在AJAX函數的回調中 - 繼續進行您想要對頁面進行的任何更改,包括重定向到新頁面。
接受的答案對我沒有產生預期的結果。
Accepted answer:
onsubmit="setTimeout(function () { window.location.reload(); }, 10)"
將接受的答案從onsubmit更改為onclick確實解決了我的問題;
Worked for me:
onclick="setTimeout(function () { window.location.reload(); }, 3)"
可能存在不希望的結果,或者這可能不是優選的,但它對我來說是按照需要執行的。
如果將回調傳遞給 onsubmit 事件屬性,它應該可以工作。 例如,
(傳統的匿名函數)
onsubmit="function() {location.reload()}"
或者
(箭頭函數)
onsubmit="() => location.reload()"
location.refresh(true);
會有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.