我在Bootstrap中使用FormValidation插件并且很喜欢它,除了我无法弄明白一件事。我有一个带有“其他”复选框的表单。如果您选中“其他”,我想要在另一个字段中输入解释。我一直在网站上(formvalidation.io),但我找不到答案。不幸的是,我知道JS足够危险。
这是我在复选框上使用的验证器的代码。
'work_type[]': {
validators: {
choice: {
min: 1,
message: 'Please choose at least one work type.'
}
}
}
还有一个名为“other_work”的字段,如果work_type的值为“Other Work”,我想要一个字符串。
答案 0 :(得分:0)
对于任何想要解决此问题的人,请查看callback functions
你声明自己的函数是这样的:
function optionOtherValidator(value) {
if ($('#amountOther input').is(':checked');) {
return /^\w+$/.test(value);
}
return true;
},
然后在您的fields数组中,将其声明为回调:
fields: {
optionOther: {
validators: {
callback: {
message: 'Please specify a value for other',
callback: optionOtherValidator
}
}
}
...