javascript - 单击提交按钮时委托发送(jQuery)
问题描述
说我们有
$submitButton.click(function(e){
doSomething(e);
return false;
});
$submitButton
表单提交按钮的 jQuery 对象在哪里。SaydoSomething
是一个带有 AJAX 调用的异步方法。
如果我在结束return true
之前提交表单doSomething
,那么我想return false
然后一旦doSomething
结束 AJAX,在成功回调中,我想实际提交表单。
这可能使用 juste
吗?如何?
解决方案
$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;
});
推荐阅读
- git - 如何查看属于 Git 子树的文件的日志?
- perl - 如何实现自定义 Perl::PrereqScanner::NotQuiteLite 解析器
- spring-data-rest - 在 Spring 数据休息中分组
- html - html和css将它们链接在一起的问题
- c# - c#实体框架分页选择?
- sql - 如果找不到匹配项,如何加入第二个表中的默认值?
- angular - 执行 ng build --prod 时出现“预期 1-3 个参数但得到 0”错误
- php - PHP - 在 php 控制器中获取另一个函数的变量
- php - laravel orm 更新文档
- angular - canActivate - 减慢应用程序的速度。什么时候调用它?