在呈现ajax响应后执行Javascript [英] Execute Javascript after ajax response has been rendered

查看:62
本文介绍了在呈现ajax响应后执行Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在渲染ajax响应后执行一段javascript。 javascript函数是在ajax请求期间动态生成的,并且在ajax响应中。 '完成'和'成功'事件不能完成这项工作。我在Firebug控制台中检查了ajax请求,并且在执行完整的回调时没有呈现响应。

I want to execute a piece of javascript after the ajax response has been rendered. The javascript function is being generated dynamically during the ajax request, and is in the ajax response. 'complete' and 'success' events to not do the job. I inspected the ajax request in Firebug console and response hasn't been rendered when the complete callback executes.

Does not work:

      function reloadForm() {
        jQuery.ajax({
          url: "<generate_form_url>",
          type: "GET",
          complete: custom_function_with_js_in_response()
        });
      };

ajaxComplete完成工作,但它会对页面上的所有ajax调用执行。我想避免这种情况。有没有可能的解决方案?

ajaxComplete does the job, but it executes for all the ajax calls on the page. I want to avoid that. Is there a possible solution?

$('#link_form').ajaxComplete(function() {
  custom_function_with_js_in_response();
});


推荐答案

你也可以使用$ .ajax(..) .done(do_things_here());

you can also use $.ajax(..).done( do_things_here() );

$(document).ready(function() {

    $('#obj').click(function() {

    $.ajax({
        url: "<url>"
    }).done(function() {
      do_something_here();
    });
});

});

还是有另一种方式

$(document).ready(function() {

    $('#obj').click(function() {

    $.ajax({
        url: "<url>",
        success: function(data){
          do_something_with(data);
        }
    })
});

});

请利用此引擎分享您的问题并尝试解决方案。非常有效。

Please, utilize this engine for share your problem and try solutions. Its very efficient.

http://jsfiddle.net / qTDAv / 7 / (PS:这包含要尝试的样本)

http://jsfiddle.net/qTDAv/7/ (PS: this contains a sample to try)

希望提供帮助

这篇关于在呈现ajax响应后执行Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆