开发者

Rails and jQuery: Javascript on page is reloading but not executing

I am a newbie developing using开发者_JS百科 rails and jquery and having been searching everywhere for a simple example of how to do this for days. I have a page, poweroutput.html.erb, that shows the power output of a customer's solar power system for a particular date. When they input a different date, I want them to see different output, but without having to reload the page. On my page I have some javascript as well as a form (for submitting the date for which a user would like to see the power output) as follows:

<head>
  <script type="text/javascript">
    <%= render "pages/poweroutputscript" %>  # renders a partial containing all my js
  </script>
</head>

# a form is here on this page too

My page's controller action is currently as follows:

def poweroutput      
  # some code 

  if request.xhr?
    @date = params[:dt]         # dt is posted when form is submitted
  else 
    @date = Time.now.getlocal.strftime("%d.%m.%Y")
  end

  respond_to do |format|
    format.html 
    format.js { render :action "pages/poweroutput.js.erb" }
  end
end

And poweroutput.js.erb is as follows:

$("script").html("<%= escape_javascript render "pages/poweroutputscript" %>");

As is, the form submits successfully and I can see on Firebug that the js between my script tags is indeed being replaced with the new js. But the view in my browser is not updating accordingly. What am I doing wrong?


I don't think replacing the content of the <script> tag will cause it to run. Try this:

$("script").replaceWith("<script><%= escape_javascript render "pages/poweroutputscript" %></script>");

or just create a new <script> and add it to your document.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜