如何获取extjs组合框的值?

shi*_*iro 9 javascript extjs extjs4

我有一个组合框的以下代码,我如何获得在组合框中选择的值并将该值加载到变量中,并在以后使用它.

谢谢

Ext.define('Column', {
    extend: 'Ext.data.Model',
    fields: ['data1', 'Data2']
});

var store = Ext.create('Ext.data.Store', {
    model: 'Column',
    autoLoad: true,
    proxy: {
        type: 'ajax',
        url: '/data.xml',
        reader: {
            type: 'xml',
            record: 'result'
        }
    }
});

var simpleCombo = Ext.create('Ext.form.field.ComboBox', {
    store: store,
    displayField: 'data1',
    valueField: 'data1',
    width: 250,
    labelWidth: 120,
    fieldLabel: 'select a value',
    renderTo: 'simpleCombo',
    queryMode: 'local',
    typeAhead: true
});
Run Code Online (Sandbox Code Playgroud)

sra*_*sra 11

只需使用select事件即可

var simpleCombo = Ext.create('Ext.form.field.ComboBox', {
            store: store,
            displayField: 'data1',
            valueField: 'data1' ,
            width: 250,
            labelWidth: 120,
            fieldLabel: 'select a value',
            renderTo: 'simpleCombo',
            queryMode: 'local',
            typeAhead: true,
            listeners: { 
               select: function(combo, records) {
                   // note that records are a array of records to be prepared for multiselection
                   // therefore use records[0] to access the selected record
               }
        });
Run Code Online (Sandbox Code Playgroud)

API链接

评论中的其他内容:

看一下组合框的multiSelect属性.您将获得由定义的分隔符分隔的所有值,并且select事件将为您提供包含多个记录的记录数组.请注意,getValue()仅为您提供已定义的displayField,它是一个字符串而不是记录本身.因此,使用iComboValue [0]为您提供第一个字符.应始终使用所选事件访问所选记录.但是您可以将它们存储在一个数组中供以后使用,并使用任何新的选择覆盖它.

  • 在评论此类内容之前,您应该阅读完整的答案.即使事件解决方案之间存在差异,也明确提到了getValue() (3认同)

Izh*_*aki 7

您还可以使用:

var iComboValue = simpleCombo.getValue();
Run Code Online (Sandbox Code Playgroud)