Vor 1 Woche hatte ich fast das gleiche Problem, ich entwickelte eine Funktion, die Eingabe in ein JavaScript-Objekt konvertieren kann.
Es ist vielleicht mehr dann, was Sie brauchen, aber Sie einen Blick in diese nehmen:
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);
})
und den HTML-Code:
<form>
x :<input type="text" name="r.1.0" value="x">
y :<input type="text" name="r.1.1" value="y">
</form>
Ergebnis: es
{
"r":{
"1":{
"0":"x",
"1":"y"
}
}
}
Hoffnung kann dir helfen.
Hier ist die JSFiddle
http://stackoverflow.com/questions/1184624/convert-form-data-to-javascript-object-with-jquery –
danke für den Link, es half mir finden, was ich Suche nach: [GitHub] (https://github.com/macek/jquery-serialize-object) –