Rails 3.1,jQuery:在发送数据之前将数据附加到表单

发布于 2024-12-07 08:34:03 字数 1138 浏览 0 评论 0 原文

我在 此问题 /rails/jquery-ujs" rel="nofollow">jquery-rails 因为我想在发送之前向表单添加一些数据。

说明看起来非常简单:挂钩 before:send 并对表单执行您想要的操作。我这样做是这样的:

form.live "ajax:before", () ->
  form.data('params', sortableList.nestedSortable('serialize'))
  console.log "data we're sending along: " + form.data('params') # here I see the data I want to append to the form

POST虽然不包含我添加到data-params的任何数据:

Started POST "/admin/site/nav_items/render_event_response?source=menu_item_tree&type=submit" for 127.0.0.1 at 2011-09-29 20:48:11 +0800
  Processing by Admin::Site::NavItemsController#render_event_response as JS
  Parameters: {"authenticity_token"=>"sV8eEMpIVicbmT0nAfDdChgpRMyI5yR/N6m5VLk3+/Q=", "source"=>"menu_item_tree", "type"=>"submit"}
  User Load (1.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  ...snip...

我理解那个问题错了? 如何在发送表单之前将数据附加到表单中?

谢谢!

I read this issue in jquery-rails because I want to add some data to the form before sending it out.

The instructions seemed pretty simple: Hook onto before:send and do what you will to the form. I did it this way:

form.live "ajax:before", () ->
  form.data('params', sortableList.nestedSortable('serialize'))
  console.log "data we're sending along: " + form.data('params') # here I see the data I want to append to the form

The POST though doesn't contain any of that data I added to data-params:

Started POST "/admin/site/nav_items/render_event_response?source=menu_item_tree&type=submit" for 127.0.0.1 at 2011-09-29 20:48:11 +0800
  Processing by Admin::Site::NavItemsController#render_event_response as JS
  Parameters: {"authenticity_token"=>"sV8eEMpIVicbmT0nAfDdChgpRMyI5yR/N6m5VLk3+/Q=", "source"=>"menu_item_tree", "type"=>"submit"}
  User Load (1.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  ...snip...

Did I understand that issue wrong? How do I go about appending data to the form before sending it out?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小草泠泠 2024-12-14 08:34:03

查看您提到的问题

您错过了关键部分。 ajax:before 回调接受 3 个参数,您应该编辑其中之一。

你的代码应该看起来像这样(可能......我还没有测试过)

form.live "ajax:before", (event,xhr,settings) ->
  settings.data('params', sortableList.nestedSortable('serialize'))

Look at the issue you are refering to

You're missing the critical part. ajax:before callback accepts 3 parameters, one of which you should be editing editing.

Your code should look like this (probably... I haven't tested it)

form.live "ajax:before", (event,xhr,settings) ->
  settings.data('params', sortableList.nestedSortable('serialize'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文