Multiple values to Django

19 views
Skip to first unread message

Denis Makarov

unread,
Apr 2, 2016, 6:20:15 PM4/2/16
to Django users
Hello! I have a MyForm which have name, quantity and quandesc fields.

I have a basic UI form for adding this fileld.

I added button for adding another instances of field on web page. Jquery func below:

function addContainer() {
    $
("#name").clone().appendTo("#food-container");
    $
("#quantity").clone().appendTo("#food-container");
    $
("#quandesc").clone().appendTo("#food-container");
}

But after that, if I send data, I have a value of last name, quantity and quandesc fields.

How add functionality for support multiple data instances of this fields?

bob gailer

unread,
Apr 3, 2016, 7:46:59 PM4/3/16
to django...@googlegroups.com
On 4/2/2016 6:20 PM, Denis Makarov wrote:
> Hello! I have a MyForm which have name, quantity and quandesc fields.
>
> I have a basic UI form for adding this fileld.
>
> I added button for adding another instances of field on web page.
> Jquery func below:
>
> |
> functionaddContainer(){
> $("#name").clone().appendTo("#food-container");
> $("#quantity").clone().appendTo("#food-container");
> $("#quandesc").clone().appendTo("#food-container");
> }
> |
>
This gives you duplicate IDs. IDs in HTML should be unique. It appears
that the 1st occurrence of an ID is ignored, since you are getting the
value of the 2nd.

To solve this you must generate a new (unique) ID each time you clone a
field. I suggest you maintain a counter that is incremented by 1 each
time you run addContainer. Append this counter to the original ID.

Something like (I don't know the precise syntax)
|ctr = ctr + 1;
field = $("#name").clone();
field['id']="name"+ctr; ?? not sure if this is the way to set the ID
field.appendTo("#food-container");
ditto for the other 2 fields.

While you are at it how about making an array of the 3 original fields,
then applying the code to each element of that array.


Bob Gailer|
Reply all
Reply to author
Forward
0 new messages