【问题标题】:ajax form send notification jqueryajax表单发送通知jquery
【发布时间】: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();
            }
        });

这个不行:

  1. 出于任何原因,“已发送”出现在发送之前。为什么?
  2. remove() 函数在通知创建之前将其删除,但并没有完全删除,可以看到“发送”通知向下移动。如何彻底删除通知?
  3. 我真的需要将“发送”调用包装在一个函数中,仅仅因为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){...} 。我宁愿不这样做,因为在这种情况下,无论请求是否快速,都会延迟调用完整的函数。我认为这是一个相当老套的解决方案

标签: jquery ajax pnotify


【解决方案1】:

以下方式更容易 4 me。 可以帮忙:

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

【讨论】:

  • 在这种情况下,inProcess 甚至在表单发送之前就被调用了......我只是希望它出现在发送事件中
  • 你可以包括这些行: .ajaxStart(function() { inProcess(); }); .ajaxStop(function() { m.remove(); }); .ajaxError(function() { alert('error'); });
猜你喜欢
  • 2013-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-25
  • 2014-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多