如何重新渲染文本字段的 javascript onchange?
我不是计算机科学家,而是机械工程师。因此,如果我没有使用正确的词语来描述事物,我提前道歉。我会尽力说清楚。
我正在开发一个 Rails 站点,该站点显示客户太阳能逆变器的功率输出。在绘制功率输出的页面上(使用javascript),我希望该页面上的javascript(带有嵌入的ruby)在文本字段(其中有日期)发生变化时重新呈现。当客户想要查看不同日期的功率输出数据时,该页面的 JavaScript 将相应地重新呈现。
我最初尝试在控制器操作中使用 render :update
和 page.replace_html
,该操作将被称为文本字段的 onchange ,但经过进一步研究发现这不是被认为是好的做法吗?另外,更重要的是,我无法让它发挥作用(我很羞愧。非常感谢那些试图帮助我的响应者!)。
我观看了 Railscast #136,并尝试根据我的需要调整该信息。
该页面是poweroutput.html.erb
。我的所有 javascript 都在主 html 文件的脚本标记内。 poweroutput.html.erb
上文本字段元素的 html 如下(它不起作用):
<%= text_field_tag(
'dt', nil,
{ :id => 'date-field',
:class => 'dateformat-d-dt-m-dt-Y',
:onchange => remote_function(
:url => {:controller => 'pages', :action => 'poweroutput'})
})
%>
所以我认为上面的内容基本上只是在文本更改时刷新/重新渲染页面场,但什么也没发生。
请帮助我,因为我正在撕扯我的头发。我认为我的部分问题一定是我不明白 remote_function
方法调用的底层 Ajax 函数正在做什么。如果您能对此有所了解,或者建议一种完全不同的方法来重新渲染文本字段的 javascript onchange,我将非常感激!
I am not a computer scientist, but a mechanical engineer by trade. Therefore, I apologize in advance if I do not use the correct words to describe things. I will try my best to be clear.
I'm working on a Rails site that displays the power output of a customer's solar power inverter. On the page where the power output is plotted (with javascript) I would like for the javascript (with embedded ruby) on that page to be re-rendered onchange of a text field (which has a date in it). As in when the customer wants to view their power output data for a different date, the javascript for this page will be re-rendered accordingly.
I was originally trying to use render :update
and page.replace_html
in a controller action that would be called onchange of the text field, but after further research found that this is not considered good practice? Also and more importantly, I couldn't make it work (I'm so ashamed. Thank you so much to those responders who tried to help me!).
I watched Railscast #136 and am trying to adapt that information to what I need.
The page is poweroutput.html.erb
. All of my javascript is within script tags in the main html file. The html for the text field element on poweroutput.html.erb
is as follows (It does not work):
<%= text_field_tag(
'dt', nil,
{ :id => 'date-field',
:class => 'dateformat-d-dt-m-dt-Y',
:onchange => remote_function(
:url => {:controller => 'pages', :action => 'poweroutput'})
})
%>
So I thought that the above would basically just refresh/re-render the page onchange of the text field, but nothing happens.
Please help as I am tearing my hair out. I think part of my problem must be that I don't understand what the underlying Ajax function that is being called by the remote_function
method is doing. If you can shed some light on this OR suggest a completely different method of re-rendering javascript onchange of a text field, I'd be so grateful!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这里可能有两件事是错误的:
1)用div包裹你的部分,因为AJAX调用结果渲染到div:
2)在你的操作中,指定部分的完整路径:
Two things that I think might be wrong here:
1) Wrap your partial with div, as AJAX calls results render to divs:
2) In your action, specify full path to the partial:
首先,我将使用 firebug 或类似的方法来检查文本字段更改时是否正在发送请求。接下来我会检查响应是什么。如果是 500 错误,请检查应用程序日志中的堆栈跟踪。
我的猜测是,响应将是 500 错误,可能是因为您的 update_poweroutput 函数正在尝试渲染不存在的部分,我认为您需要指定,
除非您正在渲染确实存在的不同部分在页面视图文件夹中。在这种情况下,您是否在控制器中设置了所有必要的变量?
First I would use firebug or similar to check that the request was being sent when the text field changes. Next I would check to see what the response is. If it's a 500 error, check the application logs for a stack trace.
My guess is that the response will be a 500 error, probably because your update_poweroutput function is trying to render a partial that doesn't exist, I think you need to specify
unless, of course, you're rendering a different partial that does exist in the pages view folder. In this case, are you setting all the necessary variables in the controller?