jQuery更改输入序列化格式

时间:2020-09-17 14:14:10

标签: javascript php html jquery

我得到了一个具有相似输入名称的表,我将使用该表生成数组并使用php和ajax保存在后端。问题是,每当我序列化输入时,它都会以与我想要的格式不同的格式生成数组,如下所示。我该如何更改?

         <table class="table">
            <thead>
                <tr>
                    <th scope="col">Product</th>
                    <th scope="col">quantity</th>
                    <th scope="col">rate</th>
                    <th scope="col">total</th>
                    <th></th>
                </tr>
            </thead>
            <tbody id="items">
                <tr>
                    <td><input form="itemForm" type="text" class="form-control" name="product[]" required /></td>
                    <td><input form="itemForm" type="text" class="form-control" name="quantity[]" value="0.00" required /></td>
                    <td><input form="itemForm" type="text" class="form-control" name="rate[]" value="0.00" required /></td>
                    <td><input form="itemForm" type="text" class="form-control" name="total[]" value="0.00" required /></td>
                </tr>

                <tr>
                    <td><input form="itemForm" type="text" class="form-control" name="product[]" required /></td>
                    <td><input form="itemForm" type="text" class="form-control" name="quantity[]" value="0.00" required /></td>
                    <td><input form="itemForm" type="text" class="form-control" name="rate[]" value="0.00" required /></td>
                    <td><input form="itemForm" type="text" class="form-control" name="total[]" value="0.00" required /></td>
                </tr>

            </tbody>
        </table>

这是php输出:

Array
(
 [product] => Array
    (
        [0] => 1
        [1] => 2
    )

 [quantity] => Array
    (
        [0] => 0.00
        [1] => 0.00
    )

 [rate] => Array
    (
        [0] => 0.00
        [1] => 0.00
    )

 [total] => Array
    (
        [0] => 0.00
        [1] => 0.00
    )

 )

这是我想要的格式:

Array (
 0 => 
  Array (
  'product' => 'product 1',
  'quantity' => 0.0,
  'rate' => 0.00,
  'total' => 0.00
 ),
 1 => 
  Array (
  'product' => 'product 2',
  'quantity' => 0.0,
  'rate' => 0.00,
  'total' => 0.00
 ),
)

1 个答案:

答案 0 :(得分:0)

这是您查询的答案,它可以通过这种方式实现预期的输出。但这也可以动态实现(更好的方法),只需要循环行即可。在“ 0 [product]”中使用“ 0”,在“ 1 [product]”中使用“ 1”的地方,必须分别传递指针。

   <table class="table">
            <thead>
                <tr>
                    <th scope="col">Product</th>
                    <th scope="col">quantity</th>
                    <th scope="col">rate</th>
                    <th scope="col">total</th>
                    <th></th>
                </tr>
            </thead>
            <tbody id="items">
                <tr>
                    <td><input form="itemForm" type="text" class="form-control" name="0[product]" required /></td>
                    <td><input form="itemForm" type="text" class="form-control" name="0[quantity]" value="0.00" required /></td>
                    <td><input form="itemForm" type="text" class="form-control" name="0[rate]" value="0.00" required /></td>
                    <td><input form="itemForm" type="text" class="form-control" name="0[total]" value="0.00" required /></td>
                </tr>

                <tr>
                    <td><input form="itemForm" type="text" class="form-control" name="1[product]" required /></td>
                    <td><input form="itemForm" type="text" class="form-control" name="1[quantity]" value="0.00" required /></td>
                    <td><input form="itemForm" type="text" class="form-control" name="1[rate]" value="0.00" required /></td>
                    <td><input form="itemForm" type="text" class="form-control" name="1[total]" value="0.00" required /></td>
                </tr>

            </tbody>
        </table>