添加form.submit事件以确保在提交表单之前发布帖子?

Zac*_*urt 4 javascript ajax jquery

我有一张表格.我想使用jquery添加一个事件处理程序.

$("form").submit(function blah() {
   $.post('ping-me-please.php', params, function (response) {
       // only submit the form once this goes through!
   });

}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

Dou*_*ner 5

像这样:

$("form").submit(function (e) {
    var form = $(this);

    if (!form.data('pinged')) {
        e.preventDefault(); // Cancel the submit
        $.post('ping-me-please.php', params, function (response) {
            // only submit the form once this goes through!

            // First we set the data, then we trigger the submit again.
            form.data('pinged', true).submit();
        });
    }
}
Run Code Online (Sandbox Code Playgroud)