2014-02-05 38 views
0

这可能吗?是否有可能通过jQuery.post()有两个成功的回调函数?

一旦我的jQuery.post成功,而不是只有一个成功回调我有两个。

例如

一旦我的形式成功地发布了一个名为“#msg”空div给出的风格和内容给出一个所谓的“色块”空div样式的数据。

代码到目前为止

$('#form1').submit(function(e) 
{ 
e.preventDefault(); 
$.ajax({ 
    type: 'POST', 
    url: 'indextest1.php', 
    data: $("#form1").serialize(), 
    success: function(response) { 
$('#msg').html("<div style='border: 1px solid black; padding:15px 50px 15px 20px; width:437px; border-radius: 8px; background:#D3EDD3 url(img/accepted_48.png) no-repeat 450px center;'>Now sit back and relax.....</div>"); 

    } 
    }); 
}); 

任何帮助或指针将不胜感激。

我试过的东西,并没有工作!

增加另一个回调

$('#form1').submit(function(e) 
{ 
e.preventDefault(); 
$.ajax({ 
    type: 'POST', 
    url: 'indextest1.php', 
    data: $("#form1").serialize(), 
    success: function(response) { 


$('#msg').html("<div style='border: 1px solid black; padding:15px 50px 15px 20px; width:437px; border-radius: 8px; background:#D3EDD3 url(img/accepted_48.png) no-repeat 450px center;'>Now sit back and relax</div>"); 

$("#colour-block").html("<div style='border: 1px solid black; padding:15px 50px 15px 20px; width:437px; border-radius: 8px; background:#D3EDD3 url(img/accepted_48.png) no-repeat 450px center;'>bla bla</div>"); 
    } 
    }); 
}); 

使用无极接口

$('#form1').submit(function(e) 
{ 
e.preventDefault(); 
    var ajax = $.ajax({ 
     type : 'POST', 
     url : 'indextest1.php', 
     data : $("#form1").serialize() 
    }).done(function(response) { 

$("#msg").html('<div style='border: 1px solid black; padding:15px 50px 15px 20px; width:437px; border-radius: 8px; background:#D3EDD3 url(img/accepted_48.png) no-repeat 450px center;'>Now sit back and relax while we go to work on your behalf, we\'ll keep you updated with information on our results and if you have any questions then we welcome your calls or emails on 078675675446 or [email protected]</div>'); 

$("#colour-block").html('<div style='border: 1px solid black; padding:15px 50px 15px 20px; width:437px; border-radius: 8px; background:#D3EDD3 url(img/accepted_48.png) no-repeat 450px center;'>Now sit back and relax while we go to work on your behalf, we\'ll keep you updated with information on our results and if you have any questions then we welcome your calls or emails on 078675675446 or [email protected]</div>'); 
    }); 
    }); 
}); 
+2

难道你不能只是在同一个函数中更新你的第二个div? – Bigood

+0

我不知道,可能。我会怎么做呢?喜欢这个? $('#msg'&&'#color-block')。html我最近才开始使用/学习JavaScript,所以不太确定。我看过api文档,并没有提到这样的事情。 –

+0

哦,我只是添加一个新的线? $('#color-block')。html ............ –

回答

1

就一举两得:

$('#form1').submit(function(e) 
{ 
e.preventDefault(); 
$.ajax({ 
    type: 'POST', 
    url: 'indextest1.php', 
    data: $("#form1").serialize(), 
    success: function(response) { 
     $('#msg').html("<div style='border: 1px solid black; padding:15px 50px 15px 20px; width:437px; border-radius: 8px; background:#D3EDD3 url(img/accepted_48.png) no-repeat 450px center;'>Now sit back and relax.....</div>"); 
     $('.colour-block').css({/*css styles here*/}); 
    } 
    }); 
}); 
+0

好吧,似乎添加第二行杀死整个jQuery并停止它实际运行。 –

+0

@DanCundy仔细检查分号,结尾括号,结尾引号和其他语法。它应该工作。另外,确保你知道jQuery的.css()的语法是什么。它与纯CSS不同:http://stackoverflow.com/questions/447197/how-to-define-multiple-css-attributes-in-jquery – zsaat14

+0

不应该有任何额外的分号,结束括号,结束报价等,因为我只增加了一条额外的线,但显然改变了我的需求。 –

5

如果您使用的承诺和donefailalways,你可以添加你想要尽可能多的,你可以尽你所能地做到每个人都可以做到

$('#form1').submit(function(e) { 
    e.preventDefault(); 
    var ajax = $.ajax({ 
     type : 'POST', 
     url : 'indextest1.php', 
     data : $("#form1").serialize() 
    }).done(function(response) { 
      $('#msg').html("<div style='border: 1px solid black; padding:15px 50px 15px 20px; width:437px; border-radius: 8px; background:#D3EDD3 url(img/accepted_48.png) no-repeat 450px center;'>Now sit back and relax.....</div>"); 
      $('#msg2').text('something else'); 
      $('#msg3').css('color', 'red'); 
    }).done(function() { 
      $('.class').text('another callback') 
      $('.class').append('<p>Not sure why you would need it ?</p>') 
    }); 

    ajax.done(function() { 
     $('.class2').txt('This is the same') 
     $('.class3').txt('you can store it in a variable') 
    }); 

    ajax.fail(function() { 
     // this fires if something goes wrong 
    }); 

    ajax.always(function() { 
     // this always fires, both on success and failure 
    }); 
}); 
+0

嘿,adeneo,我正要为“延期对象jQuery的范围”找到任何东西。令我感到困惑的是,如果我有几个AJAX调用,我怎么知道决定发送到哪里?既然你不能命名延迟,至少不会延迟inbuild,对吧?但是有没有一个范围,就像任何变量中的延期? – loveNoHate

+0

@dollarvar - deferred是从函数返回的,所以它没有真正的名字可以引用,但是你可以将它设置为一个变量,就像在上面的答案中一样,或者将它返回到你可以捕获的地方它等。 – adeneo

+0

嗯,我得到的变量的东西,没有看你的代码,只是想看到延期使用。 ;)但是,你的回报是什么意思?你如何从'$ .ajax()'返回延期? – loveNoHate

0

您可以更新相同功能的第二个div。

您也可以尝试外部化你的CSS,并给予你内心的div一些ID(或类,如你所愿),以他们的风格:

success: function(response) { 
    //Create the inner div with the appropriate id, for css styling 
    var msg_inner = $('<div>',{id:'msg-inner', html:'Now sit back and relax...'}); 
    //Same thing for your colour-block div 
    var colour_block_inner = $('<div>',{id:'colour_block-inner', html:'Blah'});  

    $('#msg').append(msg_inner); 
    $('#colour-inner').append(colour_block_inner);  
} 

用下面的CSS外化:

#msg-inner{ 
    border: 1px solid black; 
    padding:15px 50px 15px 20px; 
    width:437px; 
    border-radius: 8px; 
    background:#D3EDD3 url(img/accepted_48.png) no-repeat 450px center; 
} 

#colour-block-inner{ 
    /* Your future style here */ 
} 
+0

原谅我的无知/缺乏经验,外化有什么好处?对我来说,这似乎是一个过于复杂。此外,我已经尝试像你说的加入一秒,但它似乎完全杀死阿贾克斯。 –

+0

@DanCundy这就是CSS的重点,将风格与内容分开;关于'ajax kill',你能否通过提供JS错误来阐述(打开你的浏览器的检查器)? – Bigood

+0

我使用萤火虫,它似乎没有错误?我在哪里看控制台?在各种测试中,我似乎认为它只是接受一个“成功”的动作,而代码是这样写的。 adeneo的答案也许是唯一的办法吗? –

相关问题