【发布时间】:2013-09-12 15:19:07
【问题描述】:
我有一个通过 jquery ajax 发送的表单。我想要实现的是: 在发送表单之前应该有一个通知“发送”,在请求完成后,这个消息应该消失并且应该有一个消息“发送”。我正在使用 pnotify。 到目前为止我尝试的是:
inProcess = function(){
m = $.pnotify({
title: "sending"
});
return true;
}
event.preventDefault();
$.ajax('',{
method: "POST",
data: myform.serialize,
beforeSend: inProcess,
complete: function(response){
$.pnotify({
title: "Sent",
type: "success",
opacity: "0.8",
delay: 5000
});
m.remove();
}
});
这个不行:
- 出于任何原因,“已发送”出现在发送之前。为什么?
- remove() 函数在通知创建之前将其删除,但并没有完全删除,可以看到“发送”通知向下移动。如何彻底删除通知?
- 我真的需要将“发送”调用包装在一个函数中,仅仅因为
beforeSend需要true作为返回码吗?还是有更优雅的方式?特别是我不喜欢全局的m变量
编辑:
根据一些 cmets 和一些 google' ing,我将代码更改为:
inProcess = function(){
m = $.pnotify({
title: "sending"
});
return true;
}
event.preventDefault();
$.ajax('',{
method: "POST",
data: myform.serialize,
beforeSend: inProcess,
complete: setTimeout(function(response){
$.pnotify({
title: "Sent",
type: "success",
opacity: "0.8",
delay: 5000
});
$.pnotify_remove_all(),1000)
}
});
由于上述问题,这似乎有效但并不令人满意
【问题讨论】:
-
谢谢。我猜你的意思是
complete: setTimeout(function(response){...}。我宁愿不这样做,因为在这种情况下,无论请求是否快速,都会延迟调用完整的函数。我认为这是一个相当老套的解决方案