我正在使用 backbone-forms 我想知道是否可以在选择字段中向选项 ( <option>
) 添加一些样式。该应用程序非常复杂,所以这里只是一个片段:
this.schema = { xName : { title : i18n.t('x.name') + " " + i18n.t('x.name-msg'), type : 'Text', validators : [ { type : 'required', message : i18n.t('x.name-required-msg') }, { type : 'regexp', regexp : /.{3,}/, message : i18n.t('x.name-tooShort-msg') } ], }, */ ... */ selectY : { title : i18n.t('Y.project'), type : 'Select', options : this.getSomeOptions() //is defined and works well!! } /* and so on ... */
现在很明显,您不能仅向 selectY 添加样式属性以区分其选项和样式。例如。 (来自 backbone-forms 文档):
fieldAttrs
A map of attributes to add to the field, e.g. { style: 'background: red', title: 'Tooltip help' }
在我们的(或任何)架构中,您可以定义 name
和 label
对于选项。在渲染(或更具体地说:显示)表单字段时,您是否看到除了在肮脏的黑客中使用 jQuery 之外的任何其他可能性?任何建议将不胜感激。
最佳答案
没有内置的方法来执行此操作,因此您可能希望创建一个自定义编辑器来扩展 Select 并覆盖 _arrayToHtml 方法,例如:
var CustomEditor = Backbone.Form.editors.Select.extend({
arrayToHtml: 函数(数组) { ... }
});
参见 https://github.com/powmedia/backbone-forms#custom-editors
关于javascript - Backbone 形式:选择字段的样式选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23990480/