admin管理员组文章数量:1388859
I'm doing an Ajax request with CoffeeScript and then rendering a partial from my controller, but then after that the partial has been rendered it can't use the Javascript I had previously.
Example:
At the end my action of my home_controller
I have:
respond_to do |format|
format.html { render partial: 'frame' }
end
In my home.js.coffee
:
$('#plouf').click ->
alert('test')
Then in my partial _frame.html.erb
(in the home folder of the views folder)
<span id="plouf">test<span>
If I click on this span, nothing happens...
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
But if I add this line in my partial it works, but I know it's ugly since I already did it in my application.html.erb
Am I missing something, or is it normal that partial can't use any previous Javascript?
I'm doing an Ajax request with CoffeeScript and then rendering a partial from my controller, but then after that the partial has been rendered it can't use the Javascript I had previously.
Example:
At the end my action of my home_controller
I have:
respond_to do |format|
format.html { render partial: 'frame' }
end
In my home.js.coffee
:
$('#plouf').click ->
alert('test')
Then in my partial _frame.html.erb
(in the home folder of the views folder)
<span id="plouf">test<span>
If I click on this span, nothing happens...
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
But if I add this line in my partial it works, but I know it's ugly since I already did it in my application.html.erb
Am I missing something, or is it normal that partial can't use any previous Javascript?
Share Improve this question edited Apr 18, 2014 at 8:04 Romain asked Mar 26, 2014 at 23:56 RomainRomain 1,5111 gold badge10 silver badges13 bronze badges1 Answer
Reset to default 9Partials are piled into the view before javascript is run by the browser.
What you need is
$(document).on 'click', '#plouf', ->
alert('test')
This binds the event to the document, so when turbolinks or ajax reloads the HTML, your event handler is still active.
本文标签: jqueryRails 4 Partial can39t use javascript after being rendered (with ajax)Stack Overflow
版权声明:本文标题:jquery - Rails 4: Partial can't use javascript after being rendered (with ajax) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744566204a2613062.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论