[英]Converting json array into variable names/ values from nested form data to repopulate form
Hopefully I can explain this properly. 希望我能正确解释。 I have a form that gets converted to nested json from grouped form elements such as 我有一个表单,可以从分组表单元素转换为嵌套json,例如
<input type="checkbox" name="faults[slats][]" value="cracked"/>
<input type="checkbox" name="faults[slats][]" value="broken"/>
<input type="text" name="partsRequired[0][partDescription]"/>
<input type="text" name="partsRequired[0][size][]"/>
<input type="text" name="partsRequired[0][size][]"/>
<input type="text" name="partsRequired[1][partDescription]"/>
<input type="text" name="partsRequired[1][size][]"/>
<input type="text" name="partsRequired[1][size][]"/>
The resulting parsed json looks like: 生成的解析后的json看起来像:
{
faults : {
"slats": [
"cracked",
"broken"
]
},
partsRequired: [
{
"partDescription": "Fabric Ochre",
"size": ["1x5m", "1m", "2m"],
"colour": "Ochre",
"quantity": "1"
},
{
"partDescription": "",
"size": "",
"colour": "",
"quantity": ""
}
]
}
I need to be able to loop through this data and repopulate the form. 我需要能够遍历这些数据并重新填充表格。 I have a function that recreates the variable names and values in a way that allows me to do that for teh first set of data 'faults'. 我有一个函数可以重新创建变量名和值,从而使我可以对第一组数据“故障”执行此操作。 The issue I am having is that I can't work out how to do it for the second scenario parts required where both are combined. 我遇到的问题是,我无法解决如何将两者结合在一起所需的第二个方案部分。
See fiddle: http://jsfiddle.net/JyfA2/1/ . 参见小提琴: http : //jsfiddle.net/JyfA2/1/ 。 You can see that if you pass the array to the array function it creates the correct data but passing partsRequired saves the key integer for partsRequired[0][size][] which gives me the wrong name to find and populate the element on the form. 您可以看到,如果将数组传递给数组函数,它将创建正确的数据,但是传递partsRequired会保存partsRequired [0] [size] []的键整数,这给我一个错误的名称,以便在表单上查找和填充元素。
How can I get around this? 我该如何解决?
I have a bunch of things to make note of. 我有一堆事情要注意。 First off what you have in your jsfiddle is not true JSON. 首先,您在jsfiddle中拥有的不是真正的JSON。 It's object literal notation. 它是对象文字符号。 There is a difference. 它们是有区别的。 The difference being is that you can use dot notation (ex: partsRequired.size) to get and set values. 区别在于您可以使用点表示法(例如:partsRequired.size)来获取和设置值。 With actual JSON you'd have to send it to a json parsing function like jQuery has. 使用实际的JSON,您必须将其发送到jQuery具有的json解析函数。
var faults = {
"slats": [
"Over tighten slats"
],
"fabric": [
"Fraying (not overlooked)"
],
"leather": [
"Scuffed / Scratched",
"Indents / Creasing"
],
"headboard": [
"Damage Upholstery"
]
}
var partsRequired= [
{
"partDescription": "Fabric Ochre",
"size": ["1x5m","1m","2m"],
"colour": "Ochre",
"quantity": "1"
},
{
"partDescription": "",
"size": "",
"colour": "",
"quantity": ""
}
];
console.log(faults.headboard[0]);
console.log(faults.leather[0] + " & " + faults.leather[1]);
console.log(partsRequired[0].size[0]);
partsRequired[0].size[0] = 'whatever';
console.log(partsRequired[0].size[0]);
partsRequired[1].size = [];
partsRequired[1].size.push("1");
partsRequired[1].size.push("2");
partsRequired[1].size.push("3");
for(var i = 0; i < partsRequired[1].size.length; i++){
console.log(partsRequired[1].size[i]);
}
check this out to see some examples of what I mean. 检查一下,看看我的意思的一些例子。 http://jsfiddle.net/7ZB3G/ http://jsfiddle.net/7ZB3G/
EDIT: added code to put values in the right textbox http://jsfiddle.net/chUQV/5/ 编辑:添加了将代码放在正确的文本框中的代码http://jsfiddle.net/chUQV/5/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.