jQuery / javascript就像post数组一样获取表单数据

时间:2022-01-04 09:47:27

I have an HTML form and I want to get all its data in the same structure as a $_POST would. So if I have some inputs like this:

我有一个HTML表单,我希望将所有数据与$ _POST相同的结构。所以,如果我有这样的输入:

<input type="text" name=r[1][0] value="x"><input type="text" name=r[1][1] value="y">

I would like to get and object like this:

我想得到这样的对象:

obj = {r:{1:{0:x,1:y}}}

Can you please tell me there is any way I could do this? I tried serializeArray().. and other "custom" solutions with no success.

你能告诉我有什么办法吗?我尝试了serializeArray()..和其他“自定义”解决方案但没有成功。

Thank you! :)

谢谢! :)

1 个解决方案

#1


2  

1 weeks ago, i had almost same problem, i developed a function which can convert input to a JavaScript object.

1个星期前,我有几乎相同的问题,我开发了一个功能,可以将输入转换为JavaScript对象。

It's maybe more then what you need, but you can take a look in this :

它可能更符合您的需求,但您可以看一下:

    var myObj = {};

    function addValueToObj(objet, nom) {

                var pathBrackets = "";
                var regex = /\[([0-9]+)\]$/;
                nom = nom.split("=");
                var path = nom[0].split('.');

                val = nom.slice(1).join("=");

    for (var i = 0, tmp = objet; i<path.length-1; i++) {
                    pathBrackets += path[i]+".";    
                    if (path[i].match(regex)!=null) {

                        var valindex = path[i].match(regex)[1];
                        var index = path[i].match(regex).index;
                        var newPath = path[i].substring(index, 1-path[i].length);

                        if (typeof tmp[newPath] == "undefined") {
                            tmp = tmp[newPath] = [];
                            tmp = tmp[valindex] = {};
                        } else {
                            if (typeof tmp[newPath][valindex] == "undefined") {
                                tmp = tmp[newPath][valindex] = {};
                            } else {
                                tmp = tmp[newPath][valindex];
                            }
                        }
                    } else {
                        if (typeof tmp[path[i]] == "undefined") {
                            tmp = tmp[path[i]] = {};
                        } else {
                            tmp = tmp[path[i]];
                        }


        }

            }

        tmp[path[i]] = val;
}

$(document).ready(function() {
  $.each($("input"), function() {
        addValueToObj(myObj, $(this).attr('name')+"="+$(this).val());
  })
  console.log(myObj);
})

and the HTML :

和HTML:

<form>
x :<input type="text" name="r.1.0" value="x">
y :<input type="text" name="r.1.1" value="y">
</form>

Result :

{
"r":{
  "1":{
    "0":"x",
    "1":"y"
   }
  }
}

Hope it can help you.

希望它可以帮到你。

Here the JSFiddle

这里是JSFiddle

#1


2  

1 weeks ago, i had almost same problem, i developed a function which can convert input to a JavaScript object.

1个星期前,我有几乎相同的问题,我开发了一个功能,可以将输入转换为JavaScript对象。

It's maybe more then what you need, but you can take a look in this :

它可能更符合您的需求,但您可以看一下:

    var myObj = {};

    function addValueToObj(objet, nom) {

                var pathBrackets = "";
                var regex = /\[([0-9]+)\]$/;
                nom = nom.split("=");
                var path = nom[0].split('.');

                val = nom.slice(1).join("=");

    for (var i = 0, tmp = objet; i<path.length-1; i++) {
                    pathBrackets += path[i]+".";    
                    if (path[i].match(regex)!=null) {

                        var valindex = path[i].match(regex)[1];
                        var index = path[i].match(regex).index;
                        var newPath = path[i].substring(index, 1-path[i].length);

                        if (typeof tmp[newPath] == "undefined") {
                            tmp = tmp[newPath] = [];
                            tmp = tmp[valindex] = {};
                        } else {
                            if (typeof tmp[newPath][valindex] == "undefined") {
                                tmp = tmp[newPath][valindex] = {};
                            } else {
                                tmp = tmp[newPath][valindex];
                            }
                        }
                    } else {
                        if (typeof tmp[path[i]] == "undefined") {
                            tmp = tmp[path[i]] = {};
                        } else {
                            tmp = tmp[path[i]];
                        }


        }

            }

        tmp[path[i]] = val;
}

$(document).ready(function() {
  $.each($("input"), function() {
        addValueToObj(myObj, $(this).attr('name')+"="+$(this).val());
  })
  console.log(myObj);
})

and the HTML :

和HTML:

<form>
x :<input type="text" name="r.1.0" value="x">
y :<input type="text" name="r.1.1" value="y">
</form>

Result :

{
"r":{
  "1":{
    "0":"x",
    "1":"y"
   }
  }
}

Hope it can help you.

希望它可以帮到你。

Here the JSFiddle

这里是JSFiddle