Problem with jQuery in rails
How create a link_to_remote in a jQuery script where url need a parameter that is a javascript variable.
I need create a link开发者_开发技巧_to_remote in pure jQuery.
Thanks in advance
You'll want to use the :with
parameter with link_to_remote
:
link_to_remote( args[:title],
:update => args[:update],
:url => { :action => args[:action],
:id => id,
:starting => args[:starting]
},
:with => "'filter[viewer_id]=' + $('filter_viewer_id').value",
:loading => "Element.hide('#{args[:update]}');Element.show('#{args[:loading]}')",
:complete => "Element.show('#{args[:update]}');Element.hide('#{args[:loading]}')" )
Notice how I am sending the filter_viewer_id by getting it's value from a form field with jQuery. If you don't need that level of detail, just pass the name of your javascript variable.
Like this:
<%= link_to_remote('Hello', :url => "/test?id='+ id +'&test=true") %>
This will result in:
# => <a href="#" onclick="jQuery.ajax({data:'authenticity_token=' + encodeURIComponent('++FcXPJ0/jfp/IO6sgaEPxMfzOr+vkT5zrGVMMSkJVE='), dataType:'script', type:'post', url:'/test?id='+ id +'&test=true'}); return false;">Hello</a>
Very little info on your post. Are you using Rails 3? If so, did you check the jquery-rails gem out?
Best regards,
-- J. Fernandes
精彩评论