2

我有一个通过 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();
            }
        });

这个不起作用:

  1. 出于任何原因,“已发送”在发送之前出现。为什么?
  2. remove() 函数在通知创建之前将其删除,但并没有完全删除,可以看到“发送”通知向下移动。如何完全删除通知?
  3. 我真的需要将“发送”调用包装在一个函数中,仅仅因为beforeSend需要一个true作为返回码吗?还是有更优雅的方式?特别是我不喜欢全局m变量

编辑

根据一些评论和一些谷歌,我将代码更改为:

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)
            }
        });

由于上述问题,这似乎可行但并不令人满意

4

1 回答 1

0

以下方式更容易 4 我。有帮助:

inProcess();

$.post(
'file.php',
{
  data: myform.serialize
},
function(response)
{
  success(response);
  $.pnotify({
   title: "Sent",
   type: "success",
   opacity: "0.8",
   delay: 5000
  });
},'json'
)
.always(function() { m.remove(); }
);
于 2013-09-12T15:29:19.360 回答