【问题标题】:how to send nested data using jquery如何使用jquery发送嵌套数据
【发布时间】:2016-02-17 11:49:10
【问题描述】:

我有我使用单选按钮填充的类别列表,并且对于每个类别我都有一个子类别列表。 when a category is selected then we can choose multiple subcategories using checkboxes.How can i wrap this data and send to my view.

这是脚本

<script type="text/javascript">
  var parameter = [] ;

  $('#brand_category').click(function(event){
        parameter.push({
          brand :   $("input[type='radio']:checked").val(),
          category: $("input[type='checkbox']:checked").val()
          })

        console.log(parameter)

        $.ajax({        
              type: "post",
              url: "{% url 'seller_details' %}",
              data: { 'parameter[]' : parameter , csrfmiddlewaretoken:'{{csrf_token}}'},
              success: function(data) {
                $('#loading-image').hide();
              },
              error: function(response, error) {
              }
          });
      });

然后在视图中

list_data = self.request.POST.get('parameter[]')

这里是html

    <div id="catmodal" class="modal fade" role="dialog">
  <div class="modal-dialog" style="width:800px">
    <!-- Modal content-->
    <div class="modal-content"style="height: 450px">
      <div class="modal-header text-center">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Select Brand & Category</h4>
      </div>
      <div class="modal-body">

        <div class="modal-left">
          <ul>
            <div class="heading">Brands</div>
          {% for brand in brands %}
            <li><input type="radio" value="{{brand.title}}" name="brand">{{brand.title}}</li>
          {% endfor %}
          </ul>
        </div>

         <div class="modal-right">
          <ul>
            <div class="heading">Categories</div>
          {% for category in categories %}
            <li><input type="checkbox" value="{{category.title}}"> {{category.title}}</li>
          {% endfor %}
          </ul>
        </div>
      </div>

    </div>
    <input type="submit" value="submit" id="brand_category">
  </div>
</div>

我怎样才能做到这一点。谢谢

【问题讨论】:

  • 请用你的 html 轰炸我们 :)
  • @madalinivascu 包含了 html
  • 你怎么知道哪个品类是哪个品牌?
  • 对于使用单选按钮的选定品牌,我需要发送选定的类别
  • 那个 html 无效。 &lt;div&gt; 不能是 &lt;li&gt; 的孩子。此外,您将永远只有一个复选框的值。逻辑需要一些修正

标签: jquery django django-models


【解决方案1】:

试试list_data = self.request.POST.getlist('parameter[]')

【讨论】:

  • 我在视图中获取对象对象...如何从中检索数据?
  • list_data['brand'] 是否允许您访问?
  • console.log(parameter)的输出是什么
  • [Object]0: Objectbrand: "dp"category: "men"proto: Objectlength: n.js:3
  • 在你看来print list_data给你什么?
猜你喜欢
  • 1970-01-01
  • 2013-07-15
  • 2018-11-15
  • 2012-01-24
  • 1970-01-01
  • 1970-01-01
  • 2016-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多