Using jQuery's serializeArray with Rails forms
Using jQuery's serializeArray with Rails forms
我正在尝试在结帐流程的最后一个屏幕上显示订单摘要(在 Rails 3 中)。在此之前没有提交任何内容,所以我使用 jQuery 的serializeArray()
从表单字段中获取所有名称和值,效果很好。我收集了各种嵌套属性,因此返回的数组相当繁忙,名称为 order[donations_attributes][0][amount_in_dollars]
.
在Javascript(或使用jQuery)中,有没有一种简单的方法将这些名称和值转换为JSON字符串(这将使生成我的摘要输出更容易使用)。例如,类似这样的内容:
{
"order": {
"donations_attributes": [
{
"amount_in_dollars": 50.95,
"category": "Some Fund"
},
{
"amount_in_dollars": 90.92,
"category": "Some Other Fund"
}
],
"billing_address_attributes": {
"first_name": "Bob",
"last_name": "Smith",
"address1": "123 Whatever Street",
"and so on": "etc"
}
}
}
请记住,我还没有提交任何东西(我也不能),所以我不能在 Ruby 中提交。有没有一种明显、直接的方法可以做到这一点,或者我需要手动解析并构建字符串?
您可以将该数据按原样传递给 JQuery.ajax(...)
中的 data
参数,parama
它将最终出现在控制器方法和视图中,并查看您调用的操作,.e.g params[:order][:donations_attributes][0][:category] == "Some Fund"
。
如果要将新 url 加载到浏览器中,可以对数据使用JQuery.serialize()
并将其附加到 url(中间带有"?)以创建具有相应查询字符串的 URL。
相关文章:
- Fighting with FRP
- issue with FB.Event.subscribe
- geolocation-marker.js conflict with markerclusterer.js
- Angular 2.0 with JavaScript or TypeScript?
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- WebComponentsJS with IE10
- 指令的模板必须只有一个根元素:With restrict E&替换true
- timeago.js with datatable and PHP
- Ajax and Json with Rails
- errors with Javascript try catch
- how to split a string with ','
- Understanding Javascript scope with "var that = this&qu
- Architecture for CPU intensive tasks with NodeJS & Socke
- How to declare a Map containing certain properties with flow
- 设置'这'在React with Inverse Data Flow中
- Unit-testing multiple use cases with Karma & Mocha.js
- Modify Javascript with C#
- 谷歌地图API v3:Initial View is Fine,but Gray Box with No Map if
- Using jQuery with classes from ES6
- Using jQuery's serializeArray with Rails forms