做一个$。员额表单动作发生之前, [英] make a $.post before form action takes place

查看:99
本文介绍了做一个$。员额表单动作发生之前,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形式,当用户点击我要运行的表单操作之前创建一个单独的PHP脚本提交按钮(进入下一个页面)被执行。

I have a form and when the user clicks the submit button I want to run a separate PHP script before the form-action (going to the next page) gets executed.

当然,我可以停止的形式,行动 EVT preventDefault(); 然后我可以解雇我的jQuery $。交呼叫,但随后我不能'简历'或撤销该preventDefault电话,据我所看到的。

Of course I can stop the form-action with evt.preventDefault(); and then I can fire my jquery $.post call but then I cannot 'resume' or undo this preventDefault call, as far as I can see.

那么,什么是执行该处理一些信息的脚本的最佳方式后,用户点击提交按钮,但在此之前,用户被重定向到表单操作标记定义的下一个页面?

So what is the best way to execute a script that process some information after a user clicks the submit button BUT before the user gets redirected to the next page defined in the form action tag?

(当然,我可以只携带过来的数据,并执行任何我想要的下一个页面上 - 但在这种情况下,我想保持独立的)。

(Of course I could just carry over the data and perform whatever I want on the next page – but in this case, I would like to keep it separate).

感谢您的任何建议!

推荐答案

您可以尝试这样的:

var posted = false;
$('form').on('submit', function(ev) {
    if ( ! posted ) {
        ev.preventDefault();
        $.post(url).done(function() {
            posted = true;
            $('form').trigger('submit');
        });
    }
    posted = false;
});

或者更简洁,使用额外的参数的:

$('form').on('submit', function(ev, posted) {
    if ( ! posted ) {
        ev.preventDefault();
        $.post(url).done(function() {
            $('form').trigger('submit', [true]);
        });
    }
});

这篇关于做一个$。员额表单动作发生之前,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆