首页 > 解决方案 > 单击提交按钮时委托发送(jQuery)

问题描述

说我们有

$submitButton.click(function(e){
    doSomething(e);
    return false;
});

$submitButton表单提交按钮的 jQuery 对象在哪里。SaydoSomething是一个带有 AJAX 调用的异步方法。

如果我在结束return true之前提交表单doSomething,那么我想return false然后一旦doSomething结束 AJAX,在成功回调中,我想实际提交表单。

这可能使用 juste吗?如何?

标签: javascriptjquery

解决方案


$submitButton.click(function(e){
    //make doSomething return a promise that you can chain off of
    doSomething(e).then(function(){
        //submit the parent form
        $submitButton.closest('form').trigger('submit');
    });

    //cancel the click and wait for the ajax to finish
    return false;
});

推荐阅读