表单中的多个相关动态选择框
有一个模型 A,has_many
B 和一个可以创建新 A 和表单的表单 添加/删除 B。即,有一个链接添加新的 B,它运行一些 Javascript 来插入另一个部分,其中填写 B 的值。一切正常。
问题
指定 B 是通过使用几个 select 选择值来完成的 盒子。问题是,根据其中选择的值, 其他选择中使用的集合的范围应限制为仅显示相关选项。这是经典案例 具有动态选择框(认为国家/地区选择框),仅具有更多 选择框,我希望所有选择框最初都出现,所以 用户可以从任何地方开始(首先选择州和国家选择框 将其集合范围缩小到可以为该状态选择的集合)。
当前方法
B 呈现在表格中,每行一个 B,我想我可以 获取新集合后使用 AJAX 替换表行(内容) 从服务器。这显然需要做好几件事。
tr
标签必须有唯一的id属性,因此可以使用replace_html。- 需要对每个选择框的更改有一个触发器
- 需要将所有选择框的值(针对该行)传递到服务器
我被困在这里,因为以下原因:
- 添加 B 需要有一些唯一的东西来标识 < code>tr 标签 作为该行上的所有选择。我可以使用
Time.now.to_i
但那不会 为链接运行的 Javascript 工作以添加新的 B,因为这将 对值进行硬编码,并且全部使用相同的值。 - 不确定我是否应该使用
observe_field
还是选择中的远程调用 表单字段助手。 - 如何获取一行中所有选择的值并远程传递 触发时到服务器?
我正在从事的项目是Rails 1.2.3并使用Prototype。请随意发布“更新的”解决方案,因为我很好奇看到不同的解决方案。
对此的任何帮助都将受到高度赞赏。提前致谢!
There is a model A that has_many
B and a form where one can create a new A and
add/remove Bs. I.e. there is a link Add new B that runs some Javascript to insert another partial where one fills in values for B. All works fine.
The problem
Specifying a B is done through selecting the values using a couple of select
boxes. The problem is that depending on a value selected in one, the
collections used in the other selects should be scoped to only show relevant options. It's the classic case
with dynamic select boxes (think country - state select boxes), only with more
select boxes and I'd like all the select boxes to appear initially, so the
user can start from anywhere (select state first and country select box
narrows down its collection to the ones that can be chosen for that state).
Current approach
The Bs are rendered in a table, one B per row and I was thinking that I could
use AJAX to replace the table row (content) after fetching the new collections
from the server. This obviously needs a couple of things in place.
tr
tag must have a unique id attribute, so replace_html can be used.- need to have a trigger on change for each select box
- need to pass all select boxes' values (for that row) to the server
I'm stuck here, because of the following:
- adding a B needs to have something unique to identify the
tr
tag as well
as all the selects on that row. I can useTime.now.to_i
but that won't
work for the Javascript run by the link to add a new B because that will
hardcode the value and all use the same. - not sure if I should go with
observe_field
or a remote call in the select
form field helper. - how to pick up the values of all selects in one row and pass that remotely
to the server when triggered?
The project I'm working on is Rails 1.2.3 and uses Prototype. Please feel free to post "newer" solutions too, because I'm curious to see different solutions.
Any help on this is highly appreciated. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这并不能回答您的全部问题,但它可能会帮助您开始。我这样处理...这是用于播放列表条目,用户可以在 div
#playlist-entry
中添加新表单,每个表单都是.new_song_form
以及隐藏字段.form_num
(我在create.js.erb
文件中使用它来告诉它隐藏/警告验证错误的表单,这并不真正相关我猜对你来说)。这个表单有点不同,因为没有一个“主要”表单需要提交,它是一堆表单都在一个页面上,但这个基本想法可能会对您有所帮助。这不是完美的编程 - 例如我应该将
form_counter
作为参数传递给函数......但它工作得很好。This doesn't answer your entire question but it might get you started. I handle it like this... this is for playlist entry, where the user can add new forms in a div
#playlist-entry
, and each form is.new_song_form
with a hidden field.form_num
(which I use in thecreate.js.erb
file to tell it which form to hide/warn of validation errors, which isn't really relevant for you I guess).This form is a little different because there's no one "major" form to submit, it's a bunch of forms all on one page, but this basic idea might help you. It's not perfect programming - for example I should be passing
form_counter
as an argument to the function... but it works perfectly.动态相关选择框
如何在php中创建依赖下拉列表?我搜索了很多然后实现了这个。这是使用 HTML、JS、PHP 简单依赖下拉框创建的。
我知道可以使用 PHP、MySQL 和 JavaScript 创建动态依赖下拉表单,
但在需要向学生授课之前我从来没有借口使用它。
Dynamic Dependent Select Boxes
How to create dependent drop down list in php? I have searched a lot then implemented this one. This is created using HTML, JS, PHP simple dependent drop down boxes.
I knew it was possible to create a dynamic dependent drop down form using PHP, MySQL, and JavaScript,
but I had never had an excuse to use one before I need to teach to my students.