我有一个组合框和一个文本框。如果从组合框中选择了某些内容,我正在尝试向文本字段添加警告通知,说明“请输入值”。但是,如果从组合框中选择“3”,我只希望警告显示在文本字段上
有什么办法可以做到吗?我不确定该怎么做。
这是我的组合框和文本字段的代码:
{
xtype:'combo',
store: ['1','2','3'],
triggerAction: 'all',
fieldLabel: 'Area',
id: 'dcArea',
width: 125,
},{
xtype:'textfield',
fieldLabel: 'Mile',
id: 'mile',
width: 125,
}
最佳答案
当然,听众需要在组合中。使用 msgTarget
自定义错误消息的显示方式。
{
xtype:'combo',
store: ['1','2','3'],
triggerAction: 'all',
fieldLabel: 'Area',
// bad practice
// id: 'dcArea',
width: 150,
// <EDIT>
msgTarget: 'under',
// </EDIT>
listeners: {
change: function(combo, value) {
// use component query to retrieve the other field
var textfield = this.up('form').down('#mile');
if (value === '3') {
textfield.markInvalid("Please enter value");
} else {
textfield.clearInvalid();
}
}
}
},{
xtype:'textfield',
fieldLabel: 'Mile',
itemId: 'mile',
width: 150
}
编辑 使用 msgTarget
组合框的属性来配置错误消息的显示方式(请参阅更新的代码示例)。
关于javascript - 添加监听器以向文本字段创建警告消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18877477/