Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rails中Ajax调用后如何执行JavaScript函数?_Javascript_Jquery_Ruby On Rails_Ajax - Fatal编程技术网

Rails中Ajax调用后如何执行JavaScript函数?

Rails中Ajax调用后如何执行JavaScript函数?,javascript,jquery,ruby-on-rails,ajax,Javascript,Jquery,Ruby On Rails,Ajax,我在rails中使用Ajax调用呈现了一些元素,但是我的另一个函数需要引用这些元素。我现在面临的问题是,除非Ajax请求实际完成,否则函数无法获取这些元素。在ajax调用之后,有没有办法在Rails中运行javascript函数 例如: fetch_cards.js.erb var cards = $('.cards'); // only fetches previous cards, not the new ones my_function( cards ); def fetch_card

我在rails中使用Ajax调用呈现了一些元素,但是我的另一个函数需要引用这些元素。我现在面临的问题是,除非Ajax请求实际完成,否则函数无法获取这些元素。在ajax调用之后,有没有办法在Rails中运行javascript函数

例如:

fetch_cards.js.erb

var cards = $('.cards'); // only fetches previous cards, not the new ones

my_function( cards );
def fetch_cards
    respond_to do |format|
        format.js
    end
end
cards\u controller.rb

var cards = $('.cards'); // only fetches previous cards, not the new ones

my_function( cards );
def fetch_cards
    respond_to do |format|
        format.js
    end
end

我不知道你的意思,但是javascript提供了监听ajax调用完成的能力。由于您已经使用jQuery,因此可以执行以下操作:

$.ajax('your/resources.json', 
    complete: function(jqXHR, status){
      // this will be executed once the server has completed your request
      my_function(cards);
    }
);

您可能希望使用更具体的回调函数扩展此函数(
success
error
),您可以在上找到更多信息。

谢谢您的帮助!这几乎修复了它,只是我删除了“url”。“complete:”也缺少“}”。:)是的,这是一个有点草率的例子,我会更新它以供将来参考!