jquery。提交不止一次? [英] jquery .submit more than once?
问题描述
我有以下代码:
$('。save')。submit(function(e){
var data ='id ='+ $(this).attr('name')+'&'+ $(this).serialize();
var messageBox = $(this).next '.message');
messageBox.html('< div class =pending>正在保存...请稍候...< / div>');
$ .post( 'save.php',data,
function(response){
messageBox.html(response).delay(3000).fadeOut('slow');
});
e.preventDefault();
});
然而,当用户再次推入save时,它很好用, code> $('。save')。submit(); 不会再次被解雇....我至少没有在<$ c $内看到挂起或成功消息c> messageBox 。
我缺少什么?
编辑
我明白了。当我 fadeOut
时,我没有淡出错误div,我淡出了整个 messageBox
div,所以一旦它是 display:none
,就没有什么东西能够带回来。
<最有可能的问题是你的AJAX没有成功返回。添加一个.error()函数。
来自jquery文档的示例代码:
//在发出请求后立即分配处理程序
//并记住此请求的jqxhr对象
var jqxhr = $ .post(example.php,function(){
alert(success);
})
.success(function(){alert(second success);})
.error(function(){alert (error);})
.complete(function(){alert(complete);});
//在这里执行其他工作...
//为
上面的请求设置另一个完成函数jqxhr.complete(function(){alert(第二完整);}); b
$ b =nofollow> http://api.jquery.com/jQuery.post/
I have the following code:
$('.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();
});
And it works great, however, when a user pushes "save" again, it seems like that $('.save').submit();
is not getting fired again.... I'm at least not seeing a pending or success message within messageBox
.
What am I missing?
EDIT
I figured it out. When I fadeOut
, I'm not fading out the error div, I'm fading out the entire messageBox
div, and so once it's display:none
, there's nothing bringing it back.
解决方案 Most likely problem is that your AJAX isn't returning succesffully. Add a .error() function to it.
Example code from jquery docs:
// 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/
这篇关于jquery。提交不止一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!