使用$(本)的.js.erb文件 - Ruby on Rails的AJAX [英] Using $(this) in .js.erb file - Ruby on Rails AJAX

查看:142
本文介绍了使用$(本)的.js.erb文件 - Ruby on Rails的AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Rails3使用jQuery,并试图做简单的Ajax调用。我有一个显示的应用程序的当前状态(在线/离线)的链接。当点击它会更新状态。

I am using Rails3 with jQuery and attempting to do simple ajax calls. I have a link that displays the current status (online/offline) of an application. When clicked it will update the status.

link_to app.status, { :controller => :apps, :action => :live_status, :id => app }, {:remote => true, :class => "#{app.status} status"}

live_status.js.erb:

live_status.js.erb:

$(this).fadeOut();
$(this).parent().html("<%= escape_javascript(status_link(@wowza_app)) %>");
$(this).fadeIn();

status_link将返回更新的link_to

status_link will return the updated link_to

不过$(这)是不工作我怎么设想。如何更新使用新的状态点击状态?

However $(this) isn't working how I envision. How can I update the clicked status with the new status?

推荐答案

你应该使用一个选择。当你有code除preTED在Ajax响应,jQuery的不知道是什么这个你指。更好地利用这样的:

you should use a selector. When you have that code interpreted in the ajax response, jQuery doesn't know what "this" are you referring. Better use something like:

$('#my-special-div').fadeOut();
$('#my-special-div').parent().html("<%= escape_javascript(status_link(@wowza_app)) %>");
$('#my-special-div').fadeIn();
$('#my-special-div').hide(2000, function () {
    $(this).remove();  // here jQuery knows you are talking about 'my-special-div'
});

这篇关于使用$(本)的.js.erb文件 - Ruby on Rails的AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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