I have a form, part of which is dynamic (using jinja templates). For example, when it renders, the number of fruits is unknown. Let's say I have the below form inputs:
<input type="text" name="{{ fruit1 }} name" value="apple">
<input type="text" name="{{ fruit1 }} size" value="5">
<input type="text" name="{{ fruit1 }} color" value="red">
<input type="text" name="{{ fruit2 }} name" value="banana">
<input type="text" name="{{ fruit2 }} size" value="7">
<input type="text" name="{{ fruit2 }} color" value="yellow">
I'd like to be able to post to Flask via AJAX the form input in the following JSON structure:
{
"fruit": [{
"id": "fruit1"
"name": "apple",
"size": "5",
"color": "red"
}, {
"id": "fruit2"
"name": "banana",
"size": "7",
"color": "yellow"
}]
}
Outside of manually shaping the data using javascript, is there a more elegant way to do this--perhaps using nested inputs in some manner?