问题描述
我正在使用 jQuery FormBuilder (https://formbuilder.online/),我需要一个自定义属性作为每种类型的复选框。因此,无论是文本区域、文本输入、复选框还是类似的,我总是想添加相同的复选框作为新属性。
我的选项如下:
options = {
scrollToFieldOnAdd: false,disableFields: ['button','file','hidden'],disabledAttrs: ['access'],// Add custom checkBox property to the form
typeUserAttrs: {
text: {
showInGroupDesc: {
type: 'checkBox',label: 'Show in Group Desc.?'
}
}
}
}
它使用了 typeUserAttrs
,不幸的是,我必须准确指定要将新属性 showInGroupDesc
添加到的字段类型。
我不想在 typeUserAttrs
中为每种类型复制/粘贴此元素 - 是否有通配符设置?
解决方法
通过在将对象 userAttrs
传递给 newAttributes
之前使用新属性 typeUserAttrs
初始化它,我自己找到了一个解决方案:
// New attribute for specified fields 'fields' below
var newAttributes = {
showInGroupDesc: {
type: 'checkbox',label: 'Show in Group Desc.?'
}
};
var userAttrs = {};
const fields = ["autocomplete","checkbox-group","date","number","radio-group","select","text","textarea"];
fields.forEach(function (item,index) {
userAttrs[item] = newAttributes;
});
// Initialise the form builder
options = {
// ... All your options here ...
typeUserAttrs: userAttrs
};