mercredi 21 décembre 2016

Django - Retrieve Values of Checkboxes

How do I retrieve the values of multiple html checkboxes and send it back to my view? The checkboxes have the same name:

<input type="checkbox" value="document_1" name="checkbox_1">
<input type="checkbox" value="document_2" name="checkbox_1">
<input type="checkbox" value="document_3" name="checkbox_1">

I'm trying to give the user the option the select multiple checkboxes and then send that data back to a Django view.

My hope is to send the data back to a Django view as a list of all the selected values with Ajax, although I'm just working out a regular form right now.

So If I selected the first and last checkbox, I would send this back to the view:

data = ["document_1", "document_3]

I have tried setting a variable like this:

var data = $('.checkbox_1:checked').val()

and I'm still working on the Ajax but haven't been able to make much progress.




Aucun commentaire:

Enregistrer un commentaire