$(this).serialize() - 如何添加值?

时间:2011-06-30 18:38:19

标签: serialization jquery

目前我有以下内容:

$.ajax({
    type: 'POST',
    url: this.action,
    data: $(this).serialize(),
});

这很好用,但是我想为数据添加一个值,所以我试过

$.ajax({
    type: 'POST',
    url: this.action,
    data: $(this).serialize() + '&=NonFormValue' + NonFormValue,
});

但是没有正确发布。关于如何将项添加到序列化字符串的任何想法?这是一个非特定于表单的全局页面变量。

8 个答案:

答案 0 :(得分:161)

虽然matt b的答案可行,但您也可以使用.serializeArray()从表单数据中获取数组,修改它,并使用jQuery.param()将其转换为url编码形式。这样,jQuery就可以为您处理额外数据的序列化。

var data = $(this).serializeArray(); // convert form to array
data.push({name: "NonFormValue", value: NonFormValue});
$.ajax({
    type: 'POST',
    url: this.action,
    data: $.param(data),
});

答案 1 :(得分:83)

而不是

 data: $(this).serialize() + '&=NonFormValue' + NonFormValue,
你可能想要

 data: $(this).serialize() + '&NonFormValue=' + NonFormValue,

如果NonFormValue可能包含任何特殊字符,您应该小心对其进行URL编码。

答案 2 :(得分:6)

首先添加项目,然后序列化:

$.ajax({
    type: 'POST',
    url: this.action,
    data: $.extend($(this), {'NonFormValue': NonFormValue}).serialize()
});

答案 3 :(得分:6)

首先不应该

data: $(this).serialize() + '&=NonFormValue' + NonFormValue,

data: $(this).serialize() + '&NonFormValue=' + NonFormValue,

其次你可以使用

url: this.action + '?NonFormValue=' + NonFormValue,

或者操作是否已包含任何参数

url: this.action + '&NonFormValue=' + NonFormValue,

答案 4 :(得分:4)

不要忘记你总能做到:

<input type="hidden" name="NonFormName" value="NonFormValue" />

以您的实际形式,根据具体情况,您的代码可能会更好。

答案 5 :(得分:1)

我们可以这样做:

data = $form.serialize() + "&foo=bar";

例如:

var userData = localStorage.getItem("userFormSerializeData");
var userId = localStorage.getItem("userId");

$.ajax({
    type: "POST",
    url: postUrl,
    data: $(form).serialize() + "&" + userData + "&userId=" + userId,
    dataType: 'json',
    success: function (response) {
        //do something
    }
});

答案 6 :(得分:0)

您可以编写一个额外的函数来处理表单数据,您应该将非表单数据作为数据值添加到表单中。例如:

<form method="POST" id="add-form">
    <div class="form-group required ">
        <label for="key">Enter key</label>
        <input type="text" name="key" id="key"  data-nonformdata="hai"/>
    </div>
    <div class="form-group required ">
        <label for="name">Ente Name</label>
        <input type="text" name="name" id="name"  data-nonformdata="hello"/>
    </div>
    <input type="submit" id="add-formdata-btn" value="submit">
</form>

然后添加此jquery以进行表单处理

<script>
$(document).onready(function(){
    $('#add-form').submit(function(event){
        event.preventDefault();
        var formData = $("form").serializeArray();
        formData = processFormData(formData);
        // write further code here---->
    });
});
processFormData(formData)
{
    var data = formData;
    data.forEach(function(object){
        $('#add-form input').each(function(){
            if(this.name == object.name){
                var nonformData = $(this).data("nonformdata");
                formData.push({name:this.name,value:nonformData});
            }
        });
    });
    return formData;
}

答案 7 :(得分:0)

这更好:

data: [$(this).serialize(),$.param({NonFormValue: NonFormValue})].join('&')