lundi 29 juin 2015

How can I group a checkbox element with a number type element and work with it in html5?

I am creating an app in django and I have the next problem:

In my html5 template, I have the next structure that represents some kids and the chocolate bars they want to eat.

{% for kid_information in result %}


            <br/><input type="checkbox" name="kid" id="kid" value="{{kid_information.name}}" />
                <label for="kid"><b>{{ kid_information.name }}</b></label>       

            Chocolate-Bars: <input type="number" name="chocbars">  


{% endfor %}
<br/><br/>

As you can see, I have a 'for' structure represent a list of kids that I have, and show their name, and a field to let the user select how many chocolate bars give to each kid. I want it to be a 'checkbox' structure, to let the user select the kids and the number of the chocbars of each kid.

This is inserted in a 'form' stucture, so that when I click on the "submit" button, I treat the selection in the correspondent view.

In my view, I know that if I do the next, I get the list of the selected checkbox items:

selected_kids = request.POST.getlist('kid')
    for selected_current_kid in selected_kids:
     ...

But the problem is that I want to get the selected kids and the specified chocolate bars with each selected kid. How can get it in the view? Is there any way to group the checkbox element "kid" and the "number" field?

Thanks!!!




Aucun commentaire:

Enregistrer un commentaire