如何通过多维数组与jQuery AJAX的职位?多维、数组、职位、jQuery

2023-09-10 13:36:30 作者:残阳锦绣、墨染天下

我一直在使用序列化()来传递早报()复选框表单数据的篮子,可容纳同一类别的多个项目。

I've been using Serialize() to pass checkbox form data with Post() for a basket that can hold multiple items of the same category.

当我张贴使用提交按钮,它正常工作与多个值传递并显示在一个类别下。

When I post them using the submit button it works fine with multiple values being passed and displayed under one category.

然而,当我使用jQuery的序列化(),它只会显示每个类别的一个项目,只有两个总类。这是一个数组的问题,但我不能工作了。

However when I used Jquery serialize() it will only show one item per category and only two categories in total. This is an array issue but I cannot work it out.

是否有其他jQuery函数,我应该使用通过一个多维数组?

Is there an alternative JQuery function i should be using to pass a multi-dimensional array?

推荐答案

jQuery将采取多维数组直接,不需要序列化。

Jquery will take multi dimensional arrays directly, no need to serialize.

var data = {
  foo:  123,
  bar:  456,
    rows: [
      {
        column1 : 'hello',
        column2 : 'hola',
        column3 : 'bonjour',.
      },
      {
        column1 : 'goodbye',
        column2 : 'hasta luego',
        column3 : 'au revoir',
      },
    ],
    test1:{
      test2: {
        test3:  'baz'
      }
    }
};

_ POST数据在你的PHP文件看起来像这样

_Post Data in your PHP file would look like this

Array
   (
    [foo] => 123
    [bar] => 456
    [rows] => Array
        (
            [0] => Array
                (
                    [column1] => hello
                    [column2] => hola
                    [column3] => bonjour
                )

            [1] => Array
                (
                    [column1] => goodbye
                    [column2] => hasta luego
                    [column3] => au revoir
                )

        )

    [test1] => Array
        (
            [test2] => Array
                (
                    [test3] => baz
                )

        )

    )

一旦你定义数据多维数组,你阿贾克斯可以很简单,比如

Once you define your data multidimensional array, your Ajax could be as simple as

$.ajax({
          type:           'post',
          cache:          false,
          url:            './ajax.php',
          data:           data
      });

如果您的帖子阵列可能有你不知道的领域,你可以很容易地访问您的帖子阵列在你的PHP文件

If your post array may have fields that you don't know about, you can access your Post array in your php file easily with

$data = file_get_contents('php://input');
$data = json_decode($data, true);