在Django中的表单上选择多个发布数据丢失

时间:2018-12-21 18:11:55

标签: javascript django django-forms

我在html页面上有一个表单列表:

 <label for="group_add_insurance">Click on name to remove </label>
      <div class="col-md-12">
        <select id='group_add_insurance' name='group_add_insurance' class='form-control' size="5" multiple='multiple'>
          {% for ins in associated_insurances %}
            <option value="{{ ins.value }}">{{ ins.label }}</option>
          {% endfor %}
        </select>
      </div>

简单的东西,有一些javascript,如果您搜索保险并单击添加,它将通过简单的jquery追加将其添加到列表中。

(我仔细检查了以下列出的帖子数据):检查chrome中的帖子响应如果您只是打开一个记录并单击提交而完全不影响选择选项列表,则会得到此响应:

csrfmiddlewaretoken: akUQuXtKKbFHZTaDUZ27WFUMWXg6bmeA4Bws7wN5s97ICZFEEHSa70UWIB8SXE7T
group_name: Some Place
group_contact: Hannah Williams
billing_address_line_one: Some Address
billing_city: Bohen
billing_state: TX
billing_zip_code: 12321
billing_phone: 555 555-5555
billing_fax: 555 555-5555
billing_contact: Some Person
billing_contact_email: someemail
mailing_address_line_one: Some Address 2
mailing_city: Abq
mailing_state: NM
mailing_zip_code: 
mailing_phone: (555) 555-5555
mailing_fax: (555) 555-5555
provider_search_name: 
provider_search_name_id: 
insurance_search_name: 
insurance_search_name_id: 
group_add_insurance: 13
group_add_insurance: 12
group_add_insurance: 6
submit: Save
group_id: 27

现在,如果我单击选择选项,它将对该元素的更改jquery分类:

  $('#group_add_insurance').change(function(){

    if (confirm('Are you sure you want to remove this?')) {
        // Save it!

      $('#group_add_insurance option:selected').remove();
      alert($("#group_add_insurance option").length) //just testing sanity

     } else {
        // Do nothing!
        alert($("#group_add_insurance option").length)  //just testing sanity

    }

  });

它确实删除了正确的东西!列表的长度还确认了删除操作没有引起您的注意,并且存在正确数量的项目。

但是检查帖子数据时,group_add_insurance不再显示在帖子数据中(两个应该显示):

csrfmiddlewaretoken: akUQuXtKKbFHZTaDUZ27WFUMWXg6bmeA4Bws7wN5s97ICZFEEHSa70UWIB8SXE7T
group_name: Some Place
group_contact: Hannah Williams
billing_address_line_one: Some Address
billing_city: Bohen
billing_state: TX
billing_zip_code: 12321
billing_phone: 555 555-5555
billing_fax: 555 555-5555
billing_contact: Some Person
billing_contact_email: someemail
mailing_address_line_one: Some Address 2
mailing_city: Abq
mailing_state: NM
mailing_zip_code: 
mailing_phone: (555) 555-5555
mailing_fax: (555) 555-5555
provider_search_name: 
provider_search_name_id: 
insurance_search_name: 
insurance_search_name_id: 
submit: Save
group_id: 27

并在代码中确认

print request.POST.getlist('group_add_insurance')

为空[]

这是在表单加载时将内容添加到列表中的方式:

 $('#group_add_insurance').append(
        $("<option></option>")
          .attr("value",$("#insurance_search_name_id").val())
          .text($("#insurance_search_name").val())
      );

我确实注意到添加了一个jquery .submit,由于某种原因,我之前的编码器在提交之前选择了所有内容。我删除了此代码只是为了进行测试,如果您加载了一个项目,则不要触摸任何东西,只需单击“保存”(提交),group_add_insurance不会再次显示,因此该列表不存在。

  $('#group_create_form').submit(function(){
    /* Before submitting the form, we need to toggle (turn on) the selected attribute */

    $('#group_add_insurance option').each(function(){
      if(!$(this).is(':selected')){
        $(this).attr('selected','selected');
      }
    });

    alert($("#group_add_insurance option").length)

    return true;
  });

所以不确定是否需要此jquery提交代码来获取选择选项,也不确定为什么它在那里时,除非我调用.change删除一个选择的选项,否则它都可以正常显示,在这种情况下,什么也没有再次回来?

0 个答案:

没有答案