javascript - 如何获得 jQuery 验证以检查具有公共前缀的所有字段?
问题描述
我正在根据先前的选择动态生成一系列字段。由于我不知道会生成多少个字段,我无法对验证脚本进行硬编码来检查它们。
但是,生成的所有字段都将具有通用格式的 ID。例如,
- 分数字段都以 id="score_uniqueValue" 开头
- stanine 字段都以 id="stanine_uniqueValue" 开头
- 'years' 字段都以 id="years_uniqueValue" 开头
- 'months' 字段都以 id="months_uniqueValue" 开头
有没有办法调整我的 jQuery 验证脚本以检查表单中具有该前缀的所有字段并为其运行标准规则?这是我用于检查单个字段的脚本。
class OpAuthSignIn {
/*
* Init Sign In Form Validation
*
*/
static initValidationScore() {
jQuery('.js-validation-scores').validate({
errorClass: 'invalid-feedback animated fadeInDown',
errorElement: 'div',
errorPlacement: (error, e) => {
jQuery(e).parents('.form-group > div').append(error);
},
highlight: e => {
jQuery(e).closest('.form-group').removeClass('is-invalid').addClass('is-invalid');
},
success: e => {
jQuery(e).closest('.form-group').removeClass('is-invalid');
jQuery(e).remove();
},
rules: {
'score': {
required: true,
maxlength: 3,
number: true
},
'stanine': {
required: false,
maxlength: 1,
number: true
},
'years': {
required: false,
maxlength: 2,
number: true
},
'months': {
required: false,
maxlength: 2,
number: true
}
},
messages: {
'score' : 'Enter a valid 3 digit standardised score.',
'stanine' : 'Enter a valid stanine between 1 and 9.',
'years' : 'The ability age year value needs to be valid.',
'months' : 'Enter a valid month between 1 and 11.'
}
});
}
/*
* Init functionality
*
*/
static init() {
this.initValidationScore();
}
}// Initialize when page loads
jQuery(() => { OpAuthSignIn.init(); });
解决方案
推荐阅读
- typescript - 无法在 VSCode 中选择 Typescript 作为语言
- wordpress - 从主查询中删除分类的前 3 个帖子
- python - 火星 2020 网页抓取
- firebase - 订购带有时间戳的聊天消息
- docker - 运行 ubuntu 镜像的 docker 容器
- c# - 接收实体的 POST/PUT 主体时忽略属性上的 [JsonIgnore] 属性
- reactjs - 反应导航框阴影未删除
- linux - E:运行sudo apt-get update时子进程返回错误码
- python - 使用 Python 在特定单词后删除多行
- python - 使用 Conda forge 安装 TBATS