简体   繁体   English

在 jQuery 中序列化数组

[英]Serializing an array in jQuery

How to prepare an array of element for $.ajax submit?如何为$.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 脚本。

You're not passing an array in proper format to $.param .您没有将正确格式的数组传递给$.param From the jQuery.param docs :来自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=undefined because "val1".name , "val1".value , "val2".name , and "val2".value , are all undefined.你看到undefined=undefined&undefined=undefined因为"val1".name"val1".value"val2".name"val2".value都是未定义的。 It should look something like this:它应该是这样的:

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

So you can construct the array like this (assuming your checkboxes have a name attribute):所以你可以像这样构造数组(假设你的复选框有一个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;
});

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() });
});

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM