我在这里没有的东西。一旦选择“美国”,它将摆脱该领域,即使选择其他内容,也不会返回到选择。
teacher_signup.html
{% block content %}
<h2>Sign Up As Teacher</h2>
<form method="post">
{% csrf_token %}
{% for field in form.visible_fields %}
{{ field | as_crispy_field}}
{% if field.name == "country" %}
<input type="button" value="cube" onclick="getcube()"/>
{% endif %}
{% endfor %}
<button class="btn btn-success" type="submit">Sign Up</button>
</form>
<script>
function getcube(){
var county=document.getElementById("id_country").value;
alert(county);
}
$(document).ready(function() {
$("#id_country").change(function(){
if (this.value=="US"){
$("#id_state").replaceWith();
}
});
});
</script>
{% endblock content %}
forms.py
class TeacherSignUpForm(SignupForm):
country=CountryField(blank_label='(select country)').formfield()
# this is just doing the same thing as country=forms.CharField(max_length=20, widget=forms.Select(choices=COUNTRY_CHOICES), required=True)
state=forms.CharField(max_length=20,required=True)
我想将状态字段替换为 state = forms.CharField(max_length = 20,widget = forms.Select(choices = STATE_CHOICES),required = True,strip = True) 如果country ==“ US”并且他们选择了其他选项,那么它将仅显示文本字段。
答案 0 :(得分:0)
这是我的处理方式:
state_us
字段和带有普通文本输入小部件的state
字段)。您可以根据要为国家/地区提供的初始值,将两个hidden
之一作为初始值(或者,如果国家/地区最初为空,则将它们都隐藏)。clean
方法中(即提交了一些数据),您可以根据国家/地区的值设置适当的字段hidden
。这样可以正确显示带有错误的表单。 clean
方法中,根据cleaned_data['country']
验证是否填充了正确的字段。state_us
字段的所选值复制到state
字段,并在选择其他国家/地区时将其清除。这样,提交的state
数据将始终包含状态。