【问题标题】:Django Copying Form Data to another FormDjango 将表单数据复制到另一个表单
【发布时间】:2019-01-04 04:27:52
【问题描述】:

我有两个简单的 Django 表单 PDEM 和 GUARANTOR。如果 Patient_Relationship = Self,我想弄清楚如何将数据从 PDEM 表单复制到 GUARANTOR 表单。

class PDEMForm(forms.Form):

    first_name = forms.CharField(required=True, max_length=255, widget=forms.TextInput(attrs={'placeholder': 'Mike','class':'form-control' , 'autocomplete': 'off','pattern':'[A-Za-z ]+', 'title':'Enter Characters Only '}))
    Middle_Name = forms.CharField(required=False, max_length=10)
    last_name = forms.CharField(required=True, max_length=255, widget=forms.TextInput(attrs={'placeholder': 'Smith','class':'form-control' , 'autocomplete': 'off','pattern':'[A-Za-z ]+', 'title':'Enter Characters Only '}))
    sex = forms.ChoiceField(required=True, choices=SEX)


class GUARANTORForm(forms.Form):

    Patient_Relationship = forms.ChoiceField(required=True, choices=PTREL)
    guarantor_first_name = forms.CharField(required=True, max_length=255, widget=forms.TextInput(attrs={'placeholder': 'Guarantor First Name','class':'form-control' , 'autocomplete': 'off','pattern':'[A-Za-z ]+', 'title':'Enter Characters Only '}))
    guarantor_last_name = forms.CharField(required=True, max_length=255, widget=forms.TextInput(attrs={'placeholder': 'Guarantor Last Name','class':'form-control' , 'autocomplete': 'off','pattern':'[A-Za-z ]+', 'title':'Enter Characters Only '}))
    guarantor_sex = forms.ChoiceField(required=True,choices=SEX)

if Patient_Relationship = Self copy data from PDEM form fields to GUARANTOR form fields

【问题讨论】:

    标签: python django django-forms


    【解决方案1】:

    我最终用 JavaScript 解决了这个问题

        <script type="text/javascript">
        document.querySelector("#id_guarantor-Patient_Relationship").onchange = function () {
            console.log(this.options[this.selectedIndex].value);
            if(this.options[this.selectedIndex].value == 'S'){
                document.querySelector("#id_guarantor-guarantor_first_name").value = document.querySelector("#id_patient-first_name").value
                document.querySelector("#id_guarantor-guarantor_last_name").value = document.querySelector("#id_patient-last_name").value
                document.querySelector("#id_guarantor-guarantor_sex").value = document.querySelector("#id_patient-sex").value
    
    }
        }
    </script>
    

    【讨论】:

      猜你喜欢
      • 2015-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-26
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多