簡短的回答是,這需要CSS,JavaScript和Django的混合形式。
表格
第1步是創建一個形式與條件驗證我乾淨的方法。
from django import forms
from django.utils.translation import ugettext as _
from django.forms.widgets import RadioSelect, Textarea, CheckboxSelectMultiple
from django.utils.safestring import mark_safe
class FormContact(forms.Form):
"""
The contact form
"""
choice_a = forms.ChoiceField(
label=_(u' '),
choices=(
(1, mark_safe(_(u'First Option'))),
(0, mark_safe(_(u'Second Option'))),
),
widget=RadioSelect,
initial=1
)
show_if_choice_1 = forms.CharField(
label=_(u'Choice 1 text box')
)
show_if_choice_2 = forms.CharField(
label=_(u'Choice 2 text box')
)
def clean(self):
super(forms.Form, self).clean()
if 'choice_a' in self.cleaned_data :
if self.cleaned_data['choice_a'] == '1':
if self.cleaned_data['show_if_choice_1'] == '' :
self._errors['show_if_choice_1'] = self.error_class([_(u'Please Fill out choice 1 text box.'),])
if self.cleaned_data['choice_a'] == '2':
if self.cleaned_data['show_if_choice_2'] == '' :
self._errors['show_if_choice_2'] = self.error_class([_(u'Please fill out choice 2 box'),])
return self.cleaned_data
的JS和CSS
寫一些JavaScript + CSS的基礎上,有條件的字段的值,顯示隱藏字段。
注意:如果您需要更多的指導方向,請告訴我,如果您迅速找到jist,我不想花費永遠打字。
謝謝,這似乎有幫助;我會在運行一些測試後確認你的答案:) – 2012-02-01 08:43:25