jQuery-追加序列化 [英] JQuery - Appending to Serialize

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

问题描述

我试图弄清楚如何在JQuery中的序列化方法上附加两个值.我有以下代码使用ajax提交表单,并且还有两个要附加的变量:

I am trying to figure out how to append two more values to the serialize method in JQuery. I have the following code to submit a form with ajax and have two more variables that I would like to append:

谢谢!

    ...
    var formData = $('#contact_form').serialize();
    submitForm(formData);

    // -----------------------------------------------
    // AJAX FORM SUBMIT
    // -----------------------------------------------
    function submitForm(formData){
        $.ajax({    
            type: 'POST',
            url: 'contact.php',
            data: formData,
            dataType: 'json',
            cache: false,
            timeout: 7000,
            success: function(data) {
                // display success message
                response(data.msg,'show');
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                ...
            },              
            complete: function(XMLHttpRequest, status) { 
                ...
            }
        });
    }

推荐答案

如果将serialize()更改为serializeArray(),则可以将值压入数组:

If you change serialize() to serializeArray() you can push values into the array :

var formData = $('#contact_form').serializeArray();
formData.push({ name: "<something>", value: "<somevalue>" });
submitForm(formData);

仍然可以使用$.ajax()方法以与serialize()方法相同的方式发送数据

The data can still be sent in the same way as you would with the serialize() method, using the $.ajax() method

这篇关于jQuery-追加序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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