提交表单后防止重定向 [英] Prevent redirect after form is submitted
本文介绍了提交表单后防止重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个使用PHP提交电子邮件的HTML表单,当它提交时,它会重定向到该PHP页面。有没有办法阻止这种重定向的发生?我用jQuery构建了一个我想要发生而不是重定向的动画。我试过返回false,但它不起作用。这里是我的函数:
I have an HTML form that submits an email with PHP, and when it submits, it redirects to that PHP page. Is there a way to prevent this redirect from happening? I built an animation with jQuery that I want to occur instead of redirecting. I tried return false;, but it wouldn't work. Here's my function:
$(function() {
$('.submit').click(function() {
$('#registerform').submit();
return false;
});
});
以下是开始的表单标记:
Here's the opening form tag:
<form id="registerform" action="submit.php" method="post">
并提交按钮:
And the submit button:
<input type="submit" value="SUBMIT" class="submit" />
推荐答案
你应该用ajax发布它,这会阻止它改变页面,你仍然会得到信息。
You should post it with ajax, this will stop it from changing the page, and you will still get the information back.
$(function() {
$('form').submit(function() {
$.ajax({
type: 'POST',
url: 'submit.php',
data: { username: $(this).name.value,
password: $(this).password.value }
});
return false;
});
})
请参阅为JQuery发布文档
这篇关于提交表单后防止重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文