我在rails中使用Ajax调用呈现了几个元素,但是我的另一个函数需要引用这些元素。我现在面临的问题是,除非Ajax请求实际上已经完成,否则函数无法获取这些元素。在ajax调用之后,Rails中是否有运行javascript函数的方法?
例如:
fetch_cards.js.erb
var cards = $('.cards'); // only fetches previous cards, not the new ones
my_function( cards );
cards_controller.rb
def fetch_cards
respond_to do |format|
format.js
end
end
发布于 2014-04-04 09:14:39
我不知道您的意思,但是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
)来扩展该函数,您可以找到更多关于jQuery API文档的信息。
https://stackoverflow.com/questions/22858177
复制相似问题