在 jQuery 中序列化数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4239460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:54:57  来源:igfitidea点击:

Serializing an array in jQuery

jqueryform-submitform-serialize

提问by arma

How to prepare an array of element for $.ajaxsubmit?

如何准备要$.ajax提交的元素数组?

Here images return ["val1","val2"], but after I use $.param(images)I get the following:

这里的图像返回["val1","val2"],但在我使用后,$.param(images)我得到以下信息:

undefined=undefined&undefined=undefined

未定义=未定义&未定义=未定义


Here is my code:


这是我的代码:

$('#rem_images').click(function() {
    var images = new Array();
    $('.images_set_image input:checked').each(function(i) {
        images[i] = $(this).val();
    });
    alert($.param(images));

    return false;
}


Generally the idea is to check the images to delete on the page, then on a button click loop trough all images checked and serialize an array for submit over AJAX to the PHP script.


通常的想法是检查要在页面上删除的图像,然后在按钮单击循环中遍历所有检查的图像并序列化一个数组以通过 AJAX 提交给 PHP 脚本。

回答by Matt Ball

You're not passing an array in proper format to $.param. From the jQuery.paramdocs:

您没有将正确格式的数组传递给$.param. 从jQuery.param文档

If the object passed is in an Array, it must be an array of objects in the format returned by .serializeArray().

如果传递的对象是数组,则它必须是返回格式的对象数组.serializeArray()

The array should be an array of objects consisting of name/value pairs. You see undefined=undefined&undefined=undefinedbecause "val1".name, "val1".value, "val2".name, and "val2".value, are all undefined. It should look something like this:

该数组应该是由名称/值对组成的对象数组。您会看到,undefined=undefined&undefined=undefined因为"val1".name, "val1".value,"val2".name"val2".value, 都是未定义的。它应该是这样的:

[{name: 'name1', value: 'val1'}, {name: 'name2', value: 'val2'}]

So you can construct the array like this (assuming your checkboxes have a nameattribute):

所以你可以像这样构造数组(假设你的复选框有一个name属性):

$('#rem_images').click(function(){
    var images = [];
    $('.images_set_image input:checked').each(function(){
        var $this = $(this);
        images.push({name: $this.attr('name'), value: $this.val()});
    });
    alert($.param(images));
    return false;
});

Even slicker, though, is to use .map()(because functional programming is good stuff):

但是,即使更巧妙,也可以使用.map()(因为函数式编程是好东西):

$('#rem_images').click(function(){
    var images = $('.images_set_image input:checked').map(function(){
        var $this = $(this);
        return {name: $this.attr('name'), value: $this.val()};
    }).get();
    alert($.param(images));
    return false;
});

回答by casablanca

See the docs for $.param:

请参阅以下文档$.param

If the object passed is in an Array, it must be an array of objects in the format returned by .serializeArray()

如果传递的对象是数组,则必须是 .serializeArray() 返回格式的对象数组

[{name:"first",value:"Rick"},
{name:"last",value:"Astley"},
{name:"job",value:"Rock Star"}]

That means you need to generate your array in the same way:

这意味着您需要以相同的方式生成数组:

$('.images_set_image input:checked').each(function(i){
    images.push({ name: i, value: $(this).val() });
});

回答by Mohamad Hamouday

I find a great function to do that from another question https://stackoverflow.com/a/31751351/4110122

我从另一个问题中找到了一个很好的功能 https://stackoverflow.com/a/31751351/4110122

This function return array with key value pairs

此函数返回带有键值对的数组

$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }      
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

To use this just call:

要使用它,只需调用:

var Form_Data = $('form').serializeObject();
console.log(Form_Data);