在 jQuery 中序列化数组

发布于 2024-10-03 06:35:25 字数 529 浏览 2 评论 0原文

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

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

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


这是我的代码:

$('#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;
}


一般来说,想法是检查页面上要删除的图像,然后单击按钮循环检查所有检查的图像并序列化数组以通过 AJAX 提交到 PHP 脚本。

How to prepare an array of element for $.ajax submit?

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

⊕婉儿 2024-10-10 06:35:25

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

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

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

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

所以你可以像这样构造数组(假设你的复选框有一个 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;
});

不过,更巧妙的是使用 .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;
});

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

If the object passed is in an Array, it must be an array of objects in the format returned by .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. 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):

$('#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):

$('#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;
});
下壹個目標 2024-10-10 06:35:25

请参阅 文档了解 $.param

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

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

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

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

See the docs for $.param:

If the object passed is in an Array, it must be an array of objects in the format returned by .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() });
});
与往事干杯 2024-10-10 06:35:25

我从另一个问题中找到了一个很好的函数来做到这一点
https://stackoverflow.com/a/31751351/4110122

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

$.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;
};

要使用此函数,只需调用:

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

I find a great function to do that from another question
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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文