【问题标题】:Is it possible to have two success callback functions with a jQuery.post()?jQuery.post() 是否可以有两个成功回调函数?
【发布时间】:2014-03-02 16:25:03
【问题描述】:

这可能吗?

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

例如

一旦我的表单成功发布了数据,一个名为“#msg”的空 div 将被赋予样式和内容并且一个名为“color-block”的空 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>");
     }
   });
});

使用 Promise 接口

$('#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 isaaclayne@southwestcarfinder.co.uk</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 isaaclayne@southwestcarfinder.co.uk</div>');
     });
   });
});

【问题讨论】:

  • 你不能在同一个函数中更新你的第二个 div 吗?
  • 我不知道,可能。我该怎么办?像这样? $('#msg' && '#color-block').html 我最近才开始使用/学习 JavaScript,所以不太确定。我查看了 api 文档,并没有提到类似的内容。
  • 哦,我要添加一个新行吗? $('#colour-block').html............
  • @DanCundy 是的,看我的回答。

标签: javascript jquery html css


【解决方案1】:

您可以在同一函数中更新您的第二个 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 */
}

【讨论】:

  • 原谅我的无知/缺乏经验,外化有什么好处?对我来说,这似乎过于复杂了。我也试过像你说的那样添加第二个,但它似乎完全杀死了 ajax。
  • @DanCundy 这就是 CSS 的重点,将样式与内容分开;关于“ajax kill”,您能否通过提供 JS 错误来详细说明(为此打开浏览器的检查器)?
  • 我正在使用萤火虫,似乎没有错误?我在哪里看控制台?经过各种测试,当代码这样编写时,我似乎只接受一个“成功”动作。 adeneo 的回答也许是唯一的办法?
【解决方案2】:

两者都做:

$('#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*/});
     }
   });
});

【讨论】:

  • 好吧,添加第二行似乎会杀死整个 jQuery 并停止它的实际运行。
  • @DanCundy 仔细检查分号、结束括号、结束引号和其他语法。它应该工作。另外,确保你知道 jQuery 的 .css() 的语法是什么。它与纯 CSS 略有不同:stackoverflow.com/questions/447197/…
  • 不应该有任何额外的分号,结束括号,结束引号等,因为我只添加了一行,但显然改变了我的需要。
  • @DanCundy 尝试添加 alert('running');以不同的时间间隔查看 jQuery 停止运行的位置。这将缩小可能可疑的行。
  • @DanCundy 只是来告诉你:如果 javascript 停止,你可以查看 chrome 和 firebug(甚至 IE!)的 console,它告诉你是解析错误是,在哪一行! :)
【解决方案3】:

如果你使用 Promise 和 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
    });
});

【讨论】:

  • 嘿 adeneo,我正要为“延迟对象 jQuery 的范围”找到任何东西。令我困惑的是,如果我有多个 AJAX 调用,我怎么知道解析被发送到哪里?由于您不能命名延迟,至少不能使用延迟的内置,对吗?但是有没有一个范围,比如在任何变量中的延迟?
  • @dollarvar - deferred 是从函数返回的,因此它实际上没有可以引用的名称,但您可以将其设置为变量,就像上面的答案一样,或者把它放回你可以抓住它的地方等等。
  • 啊,我得到了变量的东西,没有看你的代码,只是想看看延迟使用。 ;) 但是,你的回报是什么意思?你如何从$.ajax()返回延迟的?
  • @dollarvar - 你可以返回它,例如在一个函数中,参见这个例子 -> jsfiddle.net/3Q42S
  • 是的,很好,谢谢。所以只是为了记录:它会自动返回。 ;) 不错。
猜你喜欢
  • 1970-01-01
  • 2011-08-28
  • 1970-01-01
  • 1970-01-01
  • 2014-09-21
  • 2016-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多