如何防止表单提交后重定向?
我搜索了该网站,找到了各种解决方案,其中大多数建议使用
return false;
问题是我正在使用这段代码来提交表单:
$(function() {
$("#snd").click(function() {
var dendar = "http://" + $("#inpt").val() + ":" + $("#pwd").val() + "secretsite.lol/something.xml";
$("#formen").attr("action", dendar);
$("#formen").submit();
alert(dendar);
return false;
});
});
警报只是让我知道表单已提交。谢谢
你们!
I have searched the site and I've found various solutions, most of them suggesting using
return false;
Problem is that I'm using this piece of code to submit the form:
$(function() {
$("#snd").click(function() {
var dendar = "http://" + $("#inpt").val() + ":" + $("#pwd").val() + "secretsite.lol/something.xml";
$("#formen").attr("action", dendar);
$("#formen").submit();
alert(dendar);
return false;
});
});
The alert is just there to let me know that the form has been submitted..
Thanks guys!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法阻止重定向,但您可以将表单提交到(可能是隐藏的)iframe,这样您就不会离开当前页面
如果帖子与当前页面位于同一服务器并且您不在,Ajax 会更好t 发布文件数据
You can't prevent a redirect, but you can submit the form into a (possibly hidden) iframe so you don't leave the current page
Ajax would be better if the post is to the same server as the current page and you aren't posting file data
您必须在 Submit() 调用的事件处理程序中返回 false,而不是在单击处理程序中返回 false。
正如其他人所指出的,这将完全阻止表单提交。您想要做的是收集表单数据并使用 Ajax 调用提交它。
$.post( url, { var1: value1, var2: value2 等... });
You must return false within the submit() call's event handler, not the click handler.
As pointed out by others, this will stop the form from submitting at all. What you want to do is collect the form data and submit it using an Ajax-call.
$.post( url, { var1: value1, var2: value2 etc... });
只使用 AJAX 调用和 POST 到数据处理程序怎么样?这听起来像你想要的。
What about just using an AJAX call with POST to a data handler? That sounds like what you want.
使用 jquery 1.4:
这将阻止表单实际提交。然后你就可以自由地对应该发布的数据执行任何操作(使用ajax,在页面上打印一些内容......)。
Using jquery 1.4:
This will prevent the form from actually submitting. Then you're free to do whatever with the data that was supposed to be posted (use ajax, print something on the page...).