根据在下拉列表中选择的项目,使文本框可见

时间:2014-11-07 12:42:22

标签: python django forms

我的forms.html

<fieldset class="task-info">
  {{ form_field(task_form['execution_time']) }}
  {{ form_field(task_form['admin_time']) }}
  {{ form_field(task_form['difficulty']) }}
  {{ form_field(task_form['priority']) }}
</fieldset>

我的form.py

class TaskForm(forms.ModelForm):
admin_time = forms.CharField(
            help_text=_('Enter If more than 60 minutes.'),
            required=False,
            widget=forms.TextInput(attrs={'class': 'fill-width'}))
keywords = (forms.CharField(
            help_text=_('Please use commas to separate your keywords.'),
            required=False,
            widget=forms.TextInput(attrs={'class': 'medium-field'})))

def __init__(self, *args, **kwargs):
    if kwargs['instance']:
        initial = kwargs.get('initial', {})
        initial['keywords'] = kwargs['instance'].keywords_list
        kwargs['initial'] = initial
    super(TaskForm, self).__init__(*args, **kwargs)

def _process_keywords(self, creator):
    if 'keywords' in self.changed_data:
        kw = [k.strip() for k in self.cleaned_data['keywords'].split(',')]
        self.instance.replace_keywords(kw, creator)

def clean(self):
    cleaned_data = super(TaskForm, self).clean()
    start_date = cleaned_data.get('start_date')
    end_date = cleaned_data.get('end_date')
    if start_date and end_date:
        if start_date >= end_date:
            raise forms.ValidationError(_("'End date' must be after 'Start date'"))
    return cleaned_data

def save(self, creator, *args, **kwargs):
    self.instance.creator = creator
    super(TaskForm, self).save(*args, **kwargs)
    if kwargs.get('commit', True):
        self._process_keywords(creator)
    return self.instance

class Media:
    css = {
        'all': ('css/admin_ace.css',)
    }

class Meta:
    model = Task
    fields = ('name', 'short_description', 'execution_time', 'difficulty',
              'priority', 'repeatable', 'team', 'project', 'type', 'start_date',
              'end_date', 'why_this_matters', 'prerequisites', 'instructions',
              'is_draft', 'is_invalid')
    widgets = {
        'name': forms.TextInput(attrs={'size': 100, 'class': 'fill-width'}),
        'short_description': forms.TextInput(attrs={'size': 100, 'class': 'fill-width'}),
        'instructions': AceWidget(mode='markdown', theme='textmate', width='800px',
                                  height='300px', wordwrap=True,
                                  attrs={'class': 'fill-width'}),
        'start_date': CalendarInput,
        'end_date': CalendarInput,
        'why_this_matters': forms.Textarea(attrs={'rows': 2, 'class': 'fill-width'}),
        'prerequisites': forms.Textarea(attrs={'rows': 4, 'class': 'fill-width'}),
    }

Models.py那是一个班级     class Task(CachedModel,CreatedModifiedModel,CreatedByModel):其中定义了execution_time

execution_time = models.IntegerField(
    choices=((i, i) for i in (15, 30, 45, 60)),
    blank=False,
    default=30,
    verbose_name='estimated time'
)

我想要实现的是60后应该再有一个选择,可以是“添加新值”,当选择此选项时,只会出现admin_time文本框。

任何一个如何实现这一目标?我们需要使用我假设的JavaScript

1 个答案:

答案 0 :(得分:0)

将输入框放在div中。

.text-box-div {
    display:none;
}

添加此JQuery

$(document).ready(function(){ 

        $('.text-box-div').hide(); 
        $('.add-new-value').show(); 

    $('.add-new-value').click(function(){ 
    $('.text-box-div').slideToggle(); 

    }); 
});