基于另一个选择框更新选择框 (Ruby on Rails)
我需要一个更完整的示例,了解如何根据 Ruby on Rails 中第二个选择框的结果更新选择框。我已经在此处询问过这个问题。我已经阅读了该帖子的反馈,但没有任何运气弄清楚这一点,而且我已经尝试了几个小时。有人知道更好(更完整)的例子吗?
I'm in need of a more complete example on how to update a select box based on the results of a second select box in Ruby on Rails. I asked about this already here. I've read through the feedback from that posting, but am not having any luck figuring this out, and I've been trying for hours. Anybody know of a better (and more complete) example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这通常是在 Javascript 中处理的。我不太喜欢编写 Javascript 代码,因此我在应用程序中为此所做的是使用 form_observer(Rails 助手,它使用 Prototype Javascript 库来监视表单的输入更改)并更新包含以下内容的 HTML 中的 DIV:第二个选择框,基于 AJAX 调用的结果。由于 AJAX 与我的服务器通信,我可以在 Ruby 中编写任意复杂的逻辑来呈现新的 HTML。
示例代码:
This is generally handled in Javascript. I don't particularly enjoy coding Javascript, so what I do for this in my application is to use a form_observer (a Rails helper which uses the Prototype Javascript library to watch your form for input changes) and have update a DIV in the HTML containing the second select box, based on the results of an AJAX call. Since AJAX talks to my server, I can write arbitrarily complex logic in Ruby to render the new HTML.
Example code: