我想根据下拉列表的选择显示单独的div标签,我正在使用ModelForm来创建我的模板。我不确定如何在我的ModelForm中添加javascript。
forms.py
class CustomerForm(forms.ModelForm):
name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Customer Name '}))
address = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Customer Address'}))
phone_number = forms.IntegerField(widget=forms.TextInput(attrs={'placeholder': 'Customer Phone Number '}))
email = forms.EmailField(widget=forms.TextInput(attrs={'placeholder': 'Customer Email'}))
contact_person = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Contact person'}))
# amc = forms.BooleanField(widget=forms.TextInput(attrs={'placeholder': 'type "amc" if the customer is in AMC'}))
amc_date = forms.DateField(widget=forms.TextInput(attrs={'placeholder': 'ex: Jan 20, 1996','id':'disabledInput'}))
amc_product = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Products listed in AMC'}))
# warranty = forms.BooleanField(widget=forms.TextInput(attrs={'placeholder': 'Type "warranty" if the customer is in Warranty'}))
warranty_date = forms.DateField(widget=forms.TextInput(attrs={'placeholder': 'ex: Jan 20, 1996'}))
warranty_product_list = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Products listed in warranty'}))
# on_call = forms.BooleanField(widget=forms.TextInput(attrs={'placeholder': 'Type "oncall/on call" if it is On Call'}))
# support = forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Enter: amc/warranty/oncall'}))
support = forms.ChoiceField(choices=support_choice, required=True, widget = forms.Select(attrs={'onchange' : 'customer()'}))
class Meta:
model = Customer
fields = ['name','address','phone_number','email','contact_person','support','amc_date','amc_product','warranty_date',
'warranty_product_list']
我想要显示' amc_date' &安培; ' amc_product'当从支持'的选项中选择amc时和' warranty_date' &安培; ' warranty_product_list'什么时候保修'从支持'。
的选项中选择customer_detail.html
<script type="text/javascript" src="{% static 'js/customer_detail.js' %}"></script>
<form method="post" action="" enctype="multipart/form-data">
{% csrf_token %}
{{ form|crispy }}
<input type="submit" class="btn btn-default " value="Submit">
</form>
我不确定如何在我的ModelForm中使用Javascript,请帮忙。
答案 0 :(得分:3)
Django
方式将是
class CustomerForm(forms.ModelForm):
.
.
class Media:
js = ('js/customer_detail.js',)
然后在模板中加入{{<yourformname>.media}}
,并在customer_detail.js
答案 1 :(得分:0)
我没有对此进行测试,但这是应该的(我在这里使用jQuery
)。
<强> forms.py 强>
class CustomerForm(forms.ModelForm):
name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Customer Name '}))
address = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Customer Address'}))
phone_number = forms.IntegerField(widget=forms.TextInput(attrs={'placeholder': 'Customer Phone Number '}))
email = forms.EmailField(widget=forms.TextInput(attrs={'placeholder': 'Customer Email'}))
contact_person = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Contact person'}))
# amc = forms.BooleanField(widget=forms.TextInput(attrs={'placeholder': 'type "amc" if the customer is in AMC'}))
amc_date = forms.DateField(widget=forms.TextInput(attrs={'placeholder': 'ex: Jan 20, 1996','id':'disabledInput'}))
amc_product = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Products listed in AMC'}))
# warranty = forms.BooleanField(widget=forms.TextInput(attrs={'placeholder': 'Type "warranty" if the customer is in Warranty'}))
warranty_date = forms.DateField(widget=forms.TextInput(attrs={'placeholder': 'ex: Jan 20, 1996'}))
warranty_product_list = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Products listed in warranty'}))
# on_call = forms.BooleanField(widget=forms.TextInput(attrs={'placeholder': 'Type "oncall/on call" if it is On Call'}))
# support = forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Enter: amc/warranty/oncall'}))
support = forms.ChoiceField(choices=support_choice, required=True)
class Meta:
model = Customer
fields = ['name','address','phone_number','email','contact_person','support','amc_date','amc_product','warranty_date',
'warranty_product_list']
<强> customer_detail.html 强>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<form method="post" action="" enctype="multipart/form-data">{% csrf_token %}
{{ form|crispy }}
<input type="submit" class="btn btn-default" value="Submit">
</form>
<script>
$(function(){
function hideInputs() {
$("#id_amc_date").hide();
$("#id_amc_product").hide();
$("#id_warranty_date").hide();
$("#id_warranty_product_list").hide();
}
$("#id_support").on('change', function(){
var val = $("#id_support").val();
if (val == 'amc') {
$("#id_amc_date").show();
$("#id_amc_product").show();
} else if (val == 'warranty') {
$("#id_warranty_date").show();
$("#id_warranty_product_list").show();
} else {
hideInputs();
}
});
hideInputs();
});
</script>
</html>