这是一个快速简单的,
<p>label for="id_new_user">New user:</label>
<input type="checkbox" name="new_user" id="id_new_user"
onclick="javascript:toggleDiv('user_choice');" checked/></p>
<p id="user_choice" style="display:none">
<label for="id_user">User:</label>
<select name="user" id="id_user">
<option value="" selected="selected">---------</option>
<option value="1">sam</option>
<option value="2">usertest</option>
</select>
表格.py
class WorkflowForm(forms.Form):
new_user = forms.BooleanField(required=False, initial=True)
user = ModelChoiceField(queryset=User.objects.all())
description = forms.CharField(required=False, widget=forms.Textarea)
def __init__(self, user, *args, **kwargs):
super(WorkflowForm, self).__init__(*args, **kwargs)
self.fields['user'].queryset = User.objects.all()
self.fields['user'].widget.attrs['style'] = 'display:none'
self.fields['user'].widget.attrs['id'] = 'user_choice'
self.fields['new_user'].widget.attrs['onclick'] = "javascript:toggleDiv('user_choice');"
模板
{{form.as_p}}
<script>
function toggleDiv(divId) {
$("#"+divId).toggle(500);
}
</script>