如何从javascript渲染部分

编程入门 行业动态 更新时间:2024-10-17 21:17:23
本文介绍了如何从javascript渲染部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个选择,根据不同选择中的选择填充为此,我做了'动态选择菜单'中Railscast#88推荐的内容。

I have a select that gets populated depending on the selection in a different select To do this, I did what is recommended in Railscast #88 on 'Dynamic Select Menus'.

但现在我需要渲染一个部分,传入每个选择中选择的值。 我无法弄清楚如何简单地从select中的:onchange事件触发方法调用,所以我想我需要在检测选择更改事件的javascript处理程序中调用render:partial。但我无法弄清楚如何。我在javascript方法中尝试了以下内容(在.js.erb文件中),但它没有按预期工作:

But now I need to render a partial, passing in the value selected in each of those selects. I can't figure out how to simply trigger a method call from the :onchange event in the select, so I'm thinking I need to call render :partial from within the javascript handler that detects the selects change event. But I can't figure out how. I tried the following in the javascript method (in the .js.erb file) but it doesn't work as hoped:

function staffSelected() { $('date_area').show(); <% render :partial => "calendar" -%> }

任何人都有一个很好的解决方案吗?

Anyone have a good solution?

推荐答案

您无法在JavaScript中部分内部检测更改事件。 Ruby是服务器端,JavaScript是客户端。当您的JavaScript运行时,您的Ruby已经被解析为常规的'HTML。

You can't render a partial inside of JavaScript to detect change events. Ruby is server side, and JavaScript is client side. By the time your JavaScript runs, your Ruby has already been parsed into regular ol' HTML.

您最好的选择是执行以下操作:

Your best bet is to do the following:

使用AJAX控件(有Prototype / scriptaculous)在onchange事件中命中你的服务器。您的服务器可以发回RJS以更新其他下拉列表,或它可以返回标准JSON,您可以在 onSuccess 中手动填充其他下拉列表AJAX调用的方法。

Use an AJAX control (there are Prototype/scriptaculous ones) to hit your server in the onchange event. Your server can send back RJS to update the other dropdowns, or it can return back standard JSON and you can populate the other dropdowns manually in the onSuccess method of the AJAX call.

更多推荐

如何从javascript渲染部分

本文发布于:2023-11-01 12:57:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549396.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!