简体   繁体   English

下拉菜单使用Rails / jQuery填充表单

[英]Dropdown Menu to populate Form with Rails/jQuery

I am going to do my best to describe this. 我将尽力描述这一点。 What I am looking to do is populate a form with values based on a single dropdown. 我想要做的是使用基于单个下拉列表的值填充表单。 I want to take what the user selects in the dropdown, send a request to rails with the value they selected, do some stuff in a controller based on that selection and return an object back to the form with the fields filled in. Ideally I'd like to do this with jquery. 我想接受用户在下拉菜单中选择的内容,使用他们选择的值将请求发送到rails,基于该选择在控制器中做一些事情,然后将对象返回到填充了字段的表单中。 d想用jquery做到这一点。

What you're describing on the frontend probably lends itself to and MVC or MVVM framework. 您在前端所描述的内容很可能适合MVC或MVVM框架。 I've used knockout.js for that with great success. 我已经使用基因敲除.js取得了巨大的成功。

Basically you bind the properties of a JS "viewmodel" object to various elements in the markup. 基本上,您将JS“ viewmodel”对象的属性绑定到标记中的各种元素。 When the viewmodel changes, it automatically updates the markup, and vice versa. 当视图模型更改时,它会自动更新标记,反之亦然。 When you're ready to send it to your backend, use jQuery.post() to send back a serialized version of the viewmodel. 当您准备将其发送到后端时,请使用jQuery.post()发送回序列化的viewmodel版本。 The server can then respond with data that can be used to populate various fields in the viewmodel. 然后,服务器可以使用可用于填充视图模型中各个字段的数据进行响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM