Rails 在选择元素更改时更新表单内容
我有一个带有选择框和三个文本输入框的表单。当选择框中的选项发生变化时,我想用与所选选项相对应的值更新三个文本输入框。这些值是存储在服务器上的模型对象。
我正在使用 Rails 3 并试图保持低调,但我找不到一个干净的方法来做到这一点。我想出了几个可怕的、混乱的解决方案,但还没有一个干净的解决方案。将这一切联系在一起的好方法是什么?特别是,我在选择元素从服务器请求模型对象时遇到问题。
这是生成表单的视图:
= form.select :region_id, region_options_for_select(@business_location.region), :prompt => '-- Region --'
= form.text_field :city
= form.text_field :state, :label => 'Province/State'
= form.text_field :country
所以我希望每当“地区”更改时填充城市/州/国家字段。请注意,区域也是表单本身的一部分。
I have a form with a select box and three text input boxes. When the option in the select box changes, I want to update the three text input boxes with values corresponding to the selected option. The values are a model object stored on the server.
I'm using Rails 3 and trying to keep things unobstrusive, but I can't find a clean way to do this. I've come up with a couple horrendous, messy solutions, but nothing clean yet. What would be a good way to tie this all together? In particular, I am having trouble with the select element requesting the model object from the server.
Here's the view that generates the form:
= form.select :region_id, region_options_for_select(@business_location.region), :prompt => '-- Region --'
= form.text_field :city
= form.text_field :state, :label => 'Province/State'
= form.text_field :country
So I wish to populate the city/state/country fields whenever "region" is changed. Note that region is also part of the form itself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 jQuery 中,类似这样的东西可以工作:
在 Rails 端,在 LocationsController#show 中:
大致就是这样工作的。将您拥有的任何控制器/模型名称替换为我编写的名称。
In jQuery, something like this could work:
On the Rails side, in the LocationsController#show:
That's roughly how it could work. Substitute whatever the controller/model names you have for the ones I made up.