我有一個用WTForms制作的Flask表單。它包括3個復(fù)選框和9個其他字段。如果選中三個復(fù)選框中的一個,則禁用這些字段中的7個。這只是復(fù)選框的一小部分示例,它是在復(fù)選框被選中時被禁用的字段,并且是OptionalIf類,如果選中了復(fù)選框,該類可使所選字段成為可選的:class OptionalIf(Optional): def __init__(self, otherFieldName, *args, **kwargs): self.otherFieldName = otherFieldName #self.value = value super(OptionalIf, self).__init__(*args, **kwargs) def __call__(self, form, field): otherField = form._fields.get(self.otherFieldName) if otherField is None: raise Exception('no field named "%s" in form' % self.otherFieldName) if bool(otherField.data): super(OptionalIf, self).__call__(form, field)在我的表單類中:holiday = BooleanField('Holiday?', id="holiday", validators=[Optional()])start_time = TimeField(label='Start Time', id='timepick1', format='%H:%M', validators=[OptionalIf('holiday'), OptionalIf('holiday_noAddedHours'), OptionalIf('sick')])該holiday_noAddedHours和sick是其他復(fù)選框領(lǐng)域,每一個都有自己的ID:holidayNAH和sick。由于有七個要禁用的字段,因此我必須在腳本中包含此字段:document.getElementById('holiday').onchange = function(){ document.getElementById('timepick1').disabled = this.checked; document.getElementById('timepick2').disabled = this.checked; ....}document.getElementById('holidayNAH').onchange = function(){ document.getElementById('timepick1').disabled = this.checked; document.getElementById('timepick2').disabled = this.checked; ....}然后再輸入另一個sickID。我想知道是否有可能縮短此時間,而不是document.getElementById在選中該框的情況下在其中禁用字段的3秒和7行呢?我知道擁有一個ID是不可能的,因為它getElementById獲得了第一個元素,那么我將如何處理呢?
Flask WTForms使用多個按鈕禁用多個字段
Qyouu
2021-05-14 18:15:25