[英]Django: How to retrieve serialized checkbox values from ajax call in django views?
For example in html, I have a form that contains check-boxes : 例如在html中,我有一个包含复选框的表单:
<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调用:
$.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. 由于serialze()给出的值类似于check = check1和check = check2 ,因此check的值仅包含check2,即后面的赋值。 Is there a way to get all checked values in an array?
有没有办法在数组中获取所有选中的值?
Thanks in advance! 提前致谢!
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.
如果您想获得'check'的所有值,那么您可以在视图中使用getlist。 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 这可能类似于你问题Jquery和Django多个复选框
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.