简体   繁体   English

Django:如何从django视图中的ajax调用中检索序列化的复选框值?

[英]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.

相关问题 如何在页面加载时调用 JavaScript ajax 并从 Django 中的视图中检索数据 - How can I do JavaScript ajax call on page onload and retrieve data from views in Django 无法从 Ajax GET 调用检索数据值到 Django 视图 - Not able to retrieve data values from Ajax GET call into Django view 如何通过ajax从Django模板向视图发送值? - How to send values via ajax from django template to views? 如何从模板文件夹中的ajax-html调用Django中views.py文件中的函数? - How make a call to a function in views.py file in Django from ajax-html in template folder? 如何在Django视图中从HTML / Javascript检索/访问变量以进行处理? - How to retrieve/access variable from HTML/Javascript in django views to process it? 如何从 JavaScript ajax 调用和打印来自 Django 模板中视图的返回数据 - How can I send localstorage data from JavaScript ajax call and print return data from views in Django template 如何在django中显示ajax调用查询和取回的数据的属性值 - How to display the values of the attributes of the data queried and retireved by ajax call in django 无法使用javascript检索django序列化的json - Cannot retrieve django serialized json using javascript 从JSON序列化的Django对象访问字段值 - Access Field Values from JSON serialized django object 如何通过 ajax Django 发布复选框的数据? - How to post data of checkbox by ajax Django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM