jQuery ajax:添加表单值 [英] jquery ajax : add form value

查看:55
本文介绍了jQuery ajax:添加表单值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery Ajax发布表单值.有没有一种方法可以动态添加值?该代码不起作用:

I am using jquery Ajax to post form values. Is there a way to add values dynamically ? This code doesn't work:

MY_APP_LOGIN.loginFormAttachEvent = function () {
$( "#login" ).unbind('submit').removeAttr('onsubmit').on('submit', function(event) {

        var values = $( this ).serializeArray();
        var o = { foo: 'bar' };
        //o.pageKey = MY_APP_NAV.CMS_PAGE_KEY;
        values.push(o);

        $.ajax({
            type : 'POST',
            url : '/user/loginajax',
            data : values,
            dataType : "json",
        });
    }
    return false;
});

推荐答案

我很久以前就这样做了,我很快从一个旧文件中挖了这个.抱歉,我现在无法为您测试这应该工作:

I had to do this long ago, I quickly dug this up from an old file I did it in. Sorry I can't test it for you now but this should work:

var form = $(this);
var form_data = new FormData(form[0]);
form_data.append('foo', 'bar');
form_data.serializeArray();

我认为您可以跳过序列化,可以直接发送form_data,我认为应该没问题.

I think you can skip the serialize, you can send form_data directly and it should be fine I think.

这篇关于jQuery ajax:添加表单值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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