1

For example in html, I have a form that contains check-boxes :

<label>
    <input type="checkbox" name="check" value="check1">Option A
</label>
<label>
    <input type="checkbox" name="check" value="check2">Option B
</label>

And an AJAX call like this:

 $.ajax({
    data: $(#form).serialize(),
    type: $(#form).attr('method'),
    url: $(#form).attr('action'),
    datatype:'html',
    success: function() { 
        ...
    }
});

Since serialze() gives values like check=check1&check=check2 , the value of check only contains check2, the later assignment. Is there a way to get all checked values in an array?

Thanks in advance!

2
  • 1
    This post should help with passing an array as GET data. If you don't want to use an array, just give different names to the checkbox elements. The values that exist as GET data are checked, and the ones that don't, aren't. Commented Aug 2, 2016 at 7:44
  • @BobRodes Thanks a lot! Commented Aug 2, 2016 at 7:58

1 Answer 1

3

I would like to comment but my reputation is not enough so I'll just post an answer. If you want to just get all the values of 'check' then you can use getlist in the view. Something like this:

# Sample URL
# sample.com/?check=check1&check=check2

# In the view you can do it like this
values = request.GET.getlist('check')

# values will be equal to [u'check1', u'check2']

This maybe similar to you problem Jquery and Django multiple checkbox

Sign up to request clarification or add additional context in comments.

2 Comments

Yes I just noticed that, I ended up using request.POST.getlist('check[]') instead of serialize(). Appreciate your help!
Glad it worked. BTW, thanks for flagging it as an answer, now I can comment. :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.