![](/img/trans.png)
[英]How to stop opening ACTION page after submitting FORM using JavaScript
[英]Change page after submitting the form using javascript
我有一个表单,提交后转到页面“ http://qwertyasdfgh.rozblog.com/New_Post ”(动作值)
我不想更改操作,但我想在提交后重定向到另一个页面。
我提交后尝试重定向到“ http://qwerty.rozblog.com/page/success ”,但它不起作用。
这是我试过的代码:
(HTML)
<form method="post" action="http://qwertyasdfgh.rozblog.com/New_Post" id="f1">
<input type="text" name="title" style="width:300px"><br />
<input type="text" name="pimg" style="width:300px" maxlength="3072"><br />
<textarea dir="rtl" id="post" name="post" style="width:300px;" aria-hidden="true"></textarea><br />
<input type="submit" name="postsubmit" value=" submit " style="background:#333;" onclick="location()">
</form>
(JS)
function location() {
window.location.replace("http://qwerty.rozblog.com/page/success");
}
你可以使用jquery和AJAX提交表单(或者我误解了你):
$('#f1').submit(function(e)
{
e.preventDefault();
$.post('http://qwertyasdfgh.rozblog.com/New_Post',
formDataAsJSON, //use eg. jquery form plugin
function(data)
{
window.location = 'somewhere';
}
);
});
你有两个选择。
1)使用AJAX提交该表单,并在收到服务器重定向浏览器到您所需页面的响应后。 您可以使用例如jQuery和Ajax表单插件。 代码如下所示:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script>
// wait for the DOM to be loaded
$(document).ready(function() {
// bind 'f1' form and provide a simple callback function
$('#f1').ajaxForm(function() {
window.location = "/page/success"
});
});
</script>
要么
2)你可以保留你的表格和js,并使用例如php来做一些东西后重定向用户。
New_post.php
<?php
// some stuff without printing (you cant change headers if you print something)
Header("Location: /page/success");
如果可能,您可以使用meta refresh in head
配置/ New_Post重定向到/ page / success:
<meta http-equiv="refresh" content="0; url=http://qwerty.rozblog.com/page/success">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.