如何根据上一个字段中的选择填充字段?
在我们设计的表单中,我们有一个“状态”字段,允许用户一次选择多个状态。有一个“城市”字段,应根据“州”字段中的选择进行填充。如果仅选择一个州,“城市”字段中的值将正确填充。但是,当选择多个状态时,它无法正确显示。
请建议一个 Ruby 解决方案。
In the form that we designed, we have a "States" field that allows its users to select multiple states at a time. There is a "City" field which should be populated as per the selection in the "State" field. The values in the "City" field are correctly populated if only one state is selected. However, it is not showing correctly when multiple states are selected.
Please suggest a Ruby solution for the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在
observe_field
中的:with
表达式。document.getElementById('usa_states_').value
将仅返回单个值,而不是所有选定的值。我不确定是否有一种方法可以在 Rails 中自动处理此问题,但一种解决方案是编写一个 JavaScript 函数,该函数将构建一个由逗号分隔的所有选定值组成的字符串。例如
,然后将
:with
更新为类似:然后最后在控制器操作中拆分值。
The problem is the
:with
expression in yourobserve_field
.document.getElementById('usa_states_').value
will only return a single value and not all the selected values.I'm not sure if there is a way to handle this automatically in Rails but one solution is to write a JavaScript function which will build a string of all the selected values separated by commas. e.g.
and then update the
:with
to be something like:and then finally split the values inside your controller action.