Rails不引人注意的javascript(UJS)ajax:附加数据参数之前 [英] Rails unobtrusive javascript (UJS) ajax:before append data params

查看:89
本文介绍了Rails不引人注意的javascript(UJS)ajax:附加数据参数之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在通过ajax发送之前(通过Rails UJS)向表单添加一些参数.

I'm trying to append some parameters to a form before sending via ajax (via Rails UJS).

  $(document).on("ajax:before",".form-example", function(event) {
    event.data = $(":hidden").serialize();
  });

隐藏的输入不在表单中,因此我需要收集它们,对其进行序列化,然后将其添加到请求(这是一个PUT请求)中.但是,在上面的示例中,没有参数传递给服务器,我做错了吗?如何通过隐藏输入的序列化值传递名为"items"的参数?

The hidden inputs aren't within the form, so I need to collect them, serialize the, and add them to the request (which is a PUT request). However, in the above example, no parameters are being passed to the server, am I doing something wrong? How can I pass parameter called "items" with the serialized values of hidden inputs?

推荐答案

实际上,可以这样做,但是您不使用该事件,而是修改表单本身(使用$(this)).

Looking into this, it is actually possible to do, but you do not use the event, you modify the form itself (using $(this)).

所以这样的事情可能会起作用:

So something like this could work:

$(document).on("ajax:before",".form-example", function() {
  var form = $(this);
  form.append($('<input />',{name: 'hidden_values',value: $(":hidden").serialize()}));
});

并且您应该能够使用params[:hidden_values]访问rails操作中的数据.

And you should be able to access the data in your rails action using params[:hidden_values].

这篇关于Rails不引人注意的javascript(UJS)ajax:附加数据参数之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆