微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jQuery .submit多次?

如何解决jQuery .submit多次?

| 我有以下代码
$(\'.save\').submit(function(e){
    var data = \'id=\' + $(this).attr(\'name\') + \'&\' + $(this).serialize();
    var messageBox = $(this).next(\'.message\');
    messageBox.html(\'<div class=\"pending\">Saving... please wait...</div>\');
    $.post(\'save.PHP\',data,function(response) {
        messageBox.html(response).delay(3000).fadeOut(\'slow\');
    });
    e.preventDefault();
});
而且效果很好,但是,当用户再次按下\“ save \”时,似乎ѭ1不再被触发。...我至少没有在
messageBox
中看到待处理或成功消息。 我想念什么? 编辑 我想到了。当我
fadeOut
时,我不会淡出错误div,而是正在淡化整个
messageBox
div,所以一旦once5ѭ,就没有任何东西可以将其恢复。     

解决方法

最可能的问题是您的AJAX无法成功返回。向其添加一个.error()函数。 来自jQuery文档的示例代码:
// Assign handlers immediately after making the request,// and remember the jqxhr object for this request
var jqxhr = $.post(\"example.php\",function() {
  alert(\"success\");
})
.success(function() { alert(\"second success\"); })
.error(function() { alert(\"error\"); })
.complete(function() { alert(\"complete\"); });

// perform other work here ...

// Set another completion function for the request above
jqxhr.complete(function(){ alert(\"second complete\"); });
http://api.jquery.com/jQuery.post/     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。