Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am a beginner in jquery so please bear with me. I have a jquery function that allows me to select multiple checkboxes and create a string as follows:

function getSelectedVals(){
     var tmp =[];
     $("input[name='checks']").each(function() {
     if ($(this).attr('checked'))
        checked = ($(this).val());
        tmp.push(checked);
     var filters = tmp.join(',');
     alert(filters)
     return filters;

I then call a django view function and pass the string as follows:

selected = getSelectedVals();
var myurl = "/bills/delete/?id=" + selected;
$.ajax({
    type: "GET",
    url: myurl,
    data: selected,
    cache: false

On the server I have a delete view function that iterates over the checkbox values and manipulates a list.

def delete(request):
    global myarray
    idx = request.GET[u'id']
    listidx = idx.split(',')
    for l in listidx:
        value = myarray[int(l)]
        myarray.remove(value)
    return HttpResponse("/bills/jqtut/")

The problem is that on the server all the indexes I am sending as the GET string are not being handled, only half are.

Please help me! Thanks

You can shorten your each function using $("input[name='checks']:checked").each and remove the if statement. – rahul Feb 3, 2010 at 10:40

From what I see, you do it the way around. You should set the same name on all checkboxes. I don't know why do you send it by GET, I'd suggest sending it by POST.

<input type="checkbox" name="vehicle" value="Bike" />
<input type="checkbox" name="vehicle" value="Car" />
<input type="checkbox" name="vehicle" value="Airplane" />

Then, use getlist() method in your view:

def delete(request):
    values = request.POST.getlist(u'vehicle')
    # Handling goes here.

But anyway, unless you really need to do custom stuff (however special cases are not special enough to break the rules ;), use Django forms. There already is a checkbox list OOTB. I'm not sure why do you even consider using JavaScript in this very case.

Thanks ! However I am stuck again:( I am trying to send "filters" in the above code as POST data to Django , but I cannot figure out the correct dataType. What is the correct way to post this data so that django sees the list which it can iterate ? – spyder Feb 3, 2010 at 21:43

firstly, I don't know if you need to manipulate the checkbox values into a string like you are doing. if you allocate them the same name attribute, http (or jQuery's) serialisation will do it for you.

On the Django side use getlist to grab a list, the [] accessor only gets the last value in a list of params.

Http default handling however doesn't send through the values of checkboxes that aren't checked, so you might want to build in some checks that compare the checkboxes you build vs the data you get back.

As Gavoja says above, django forms is probably a good answer for this: Specifically, you want to use MultipleChoiceField with a CheckboxSelectMultiple widget and the choices as your options:

my_field = forms.MultipleChoiceField(choices=SOME_CHOICES, widget=forms.CheckboxSelectMultiple())

also see: In Django is there a way to display choices as checkboxes?

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.