在jQuery中返回array()

时间:2016-12-01 20:45:04

标签: jquery

我有以下HTML列表

<table class="table table-bordered">
    <tbody>
        <tr>
            <th width="50" class="text-center">#</th>
            <th>Item do Pacote</th>
            <th>Valor</th>
            <th>Vencimento</th>
            <th width="50" class="text-center"></th>
        </tr>
        <tr>
            <td width="50" class="text-center">1</td>
            <td>Salgados - 100 Un</td>
            <td>R$ 150,00</td>
            <td width="200px">
            <input type="date" class="form-control" name="itens[0][data_vencimento]" id="itens[data_vencimento][]">            
            </td>
            <td width="50" class="text-center"><input type="checkbox" name="itens[0][item]" id="itens[item]" class="checados" value="150,00|1"></td>
        </tr>
                <tr>
            <td width="50" class="text-center">2</td>
            <td>Doces - 100 Un</td>
            <td>R$ 114,00</td>
            <td width="200px">
            <input type="date" class="form-control" name="itens[1][data_vencimento]" id="itens[data_vencimento][]">            
            </td>
            <td width="50" class="text-center"><input type="checkbox" name="itens[1][item]" id="itens[item]" class="checados" value="114,00|2"></td>
        </tr>
        <tr>
            <td width="50" class="text-center">3</td>
            <td>Refrigerante - 10 un</td>
            <td>R$ 85,00</td>
            <td width="200px">
            <input type="date" class="form-control array_teste" name="itens[2][data_vencimento]" id="itens[data_vencimento][]">            
            </td>
            <td width="50" class="text-center"><input type="checkbox" name="itens[2][item]" id="itens[item]" class="array_teste" value="85,00|3"></td>
        </tr>
    </tbody>
</table>

我需要通过jQuery检索字段内的所有数据。 我试着这样做:

$("#salvar_festa").click(function() {

    var itens = $(".array_teste").serializeArray();

    $.ajax({
        url: basePath + 'evento/salvar_festa',
        type: 'POST',
        dataType: 'html',
        data: {
            itens: itens
        },
    })
    .done(function(ret) {
        console.log("success");
        $('#mensagePage').html(ret);
    }); 

});

但是这样,我无法返回数组对象,它应返回如下:

[item] => Array
    (
        [0] => Array
            (
                [data_vencimento] => 2016-12-05
                [itens] => 150,00|1
            )

        [1] => Array
            (
                [data_vencimento] => 2016-12-07
                [itens] => 114,00|2
            )

        [2] => Array
            (
                [data_vencimento] => 2016-12-22
                [itens] => 85,00|3
            )               
    )

但我不知道如何解决这个问题。 在PHP的save_fest中,我有print_r($ _POST);

我通过print_r($ _POST)返回:

Array
(
    [itens] => Array
        (
            [0] => Array
                (
                    [name] => itens[0][data_vencimento]
                    [value] => 2016-12-01
                )

            [1] => Array
                (
                    [name] => itens[0][item]
                    [value] => 150,00|1
                )

            [2] => Array
                (
                    [name] => itens[1][data_vencimento]
                    [value] => 2016-12-01
                )

            [3] => Array
                (
                    [name] => itens[1][item]
                    [value] => 114,00|2
                )

            [4] => Array
                (
                    [name] => itens[2][data_vencimento]
                    [value] => 2016-12-01
                )

            [5] => Array
                (
                    [name] => itens[2][item]
                    [value] => 85,00|3
                )

        )

)

1 个答案:

答案 0 :(得分:1)

您可以使用.serialize() http://jqapi.com/#p=serialize

不是.serializeArray() http://jqapi.com/#p=serializeArray

不同之处在于:如果使用.serializeArray(),则需要遍历它们以获取正确的值,此函数通常仅用于调试(除非您出于某种原因确实需要它)