Rails 3.1 CoffeeScript/JQuery - How to change the

2019-08-06 12:02发布

问题:

I would like to change the image of a link with jquery. This is what I tried :

In my Haml file :

.selected
  = link_to image_tag('/assets/unselected.png'), {:controller => Posts, :action => "update", :selected => true}, :id => "selected_post", :remote => true, :method => :put

After some stuff in my posts_controller, here is the update.js.coffee where I tried to change the image of the link with a new one called selected.png :

$("selected_post").attr 'src', '/assets/selected.png'

I does not work and if I call an alert with :

alert $("selected_post").attr 'src'

It sends "undefined".

I think I am missing something.

回答1:

#selected_post should be your selector

# is the css shortcut for the id attribute. So this should work:

$("#selected_post").attr 'src', '/assets/selected.png'