两个Ajax调用一次只能满足一个条件

时间:2017-12-07 15:04:17

标签: ajax django django-ajax-selects

我有以下脚本将我的select选项的POST发送到名为requestaccess的url。当仅使用其中一个时,这非常有效,但是当我更改另一个字段时,POST的结果为第一个为None而第二个为正确,反之亦然。

name1

我的视图使用以下内容获取POST值:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<select title ="accesslevelid" class="form-control" id="accesslevelid" onclick="accesslevelid">
            <option value=""> Please select your access level  </option>
            <option value="7"> Facility  </option>
            <option value="5"> Division  </option>
            <option value = "3"> Corporate  </option>
            <option value = "6"> Market  </option>
            <option value = "4"> Group  </option>
</select>
<script>
$(document).ready(function () {
    $('#accesslevelid').on('click', function () {
        var accesslevelid = $(this).val();
        $.ajax({ url: "{% url 'requestaccess' %}",
                headers: { 'X-CSRFToken': '{{ csrf_token }}' },
                data: {
                  accesslevelid: accesslevelid,
                },
                type: 'POST',
                success: function (result) {
                  ;
                },
              });
      });
  });
</script>
        </div>
        <div class="col">


          <label for="phi"><h3>PHI</h3></label>

          <select class="form-control" id="phi" title = "phi" onclick="phi">
            <option value = ""> Please select if you need access to PHI data </option>
            <option value = "0"> No  </option>
            <option value = "1"> Yes  </option>

          </select>
          <script>
          $(document).ready(function () {
              $('#phi').on('click', function () {
                  var phi = $(this).val();
                  $.ajax({ url: "{% url 'requestaccess' %}",
                          headers: { 'X-CSRFToken': '{{ csrf_token }}' },
                          data: {
                            phi: phi,
                          },
                          type: 'POST',
                          success: function (result) {
                            ;
                          },
                        });
                });
            });
          </script>

但是,我的打印显示为:

selectedaccesslevel = request.POST.get('accesslevelid')
print(selectedaccesslevel)
selectedphi = request.POST.get('phi')
print(selectedphi)

我想要的结果是显示为:

None
1 or 2

or 

7, 5, 3, 6, 4
None.

1 个答案:

答案 0 :(得分:1)

也许只为两个选项编写单击功能,即每次单击任一选项时,您将同时获取选择值并将其传递给视图,如下所示:

<select class="form-control my_select" id="phi" title = "phi" >
<select title ="accesslevelid" class="form-control my_select" id="accesslevelid">

不要忘记在您的选择中添加班级名称“ my_select ”。

If CurrentProject.AllForms(frm_BT_Halbzeug.Form.loadHalbzeug(Me.hidden_bauteil_id)).IsLoaded Then
        Call Me.frm_BT_Halbzeug.Form.loadHalbzeug(Me.hidden_bauteil_id)
End If