Flask WTForms 使用多个按钮禁用多个字段
Flask WTForms disabling multiple fields with multiple buttons
我在 Flask 中有一个用 WTForms 制作的表格。它包括 3 个复选框和 9 个其他字段。如果勾选了三个复选框中的一个,其中 7 个字段将被禁用。
这里只是一个复选框的小示例,勾选复选框时禁用的字段,以及勾选复选框时使所选字段可选的 OptionalIf class:
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)
在我的表单中 class:
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
是其他复选框字段,每个都有自己的 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;
....
}
然后是 sick
ID 的另一个。
我想知道是否可以缩短它,而不是在选中该框的情况下使用 3 document.getElementById
和 7 行来禁用字段?我知道不可能有一个 ID,因为它 getElementById
获取第一个元素,那么我该怎么做呢?
感谢任何建议。
在您要更改的 ID 列表上使用 for 循环怎么样?例如:
all_ids = ['timepick1', 'timepick2'];
for (i=0; i<all_ids.length; i++){
document.getElementById(all_ids[i]).disabled = this.checked;
}
我在 Flask 中有一个用 WTForms 制作的表格。它包括 3 个复选框和 9 个其他字段。如果勾选了三个复选框中的一个,其中 7 个字段将被禁用。
这里只是一个复选框的小示例,勾选复选框时禁用的字段,以及勾选复选框时使所选字段可选的 OptionalIf class:
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)
在我的表单中 class:
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
是其他复选框字段,每个都有自己的 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;
....
}
然后是 sick
ID 的另一个。
我想知道是否可以缩短它,而不是在选中该框的情况下使用 3 document.getElementById
和 7 行来禁用字段?我知道不可能有一个 ID,因为它 getElementById
获取第一个元素,那么我该怎么做呢?
感谢任何建议。
在您要更改的 ID 列表上使用 for 循环怎么样?例如:
all_ids = ['timepick1', 'timepick2'];
for (i=0; i<all_ids.length; i++){
document.getElementById(all_ids[i]).disabled = this.checked;
}