Javascript 如何获取 extjs 组合框的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12116580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:09:05  来源:igfitidea点击:

how to get the value on extjs combo box?

javascriptextjsextjs4

提问by shiro

I have a following code for combo box, how can I get the value that is selected in the combobox and load that value into a variable, and use it later.

我有以下组合框代码,如何获取组合框中选择的值并将该值加载到变量中,然后再使用它。

Thank you

谢谢

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
});

回答by sra

Simply use the select event

只需使用 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
               }
        });

API Link

接口链接

Additional content from the comments:

评论中的补充内容:

Take a look at the multiSelectproperty of the combobox. You get all values separated by a defined delimiter and the select event will give you a records array with more that one record. Note the that getValue() only give you the defined displayField which is a string and not the record itself. So using iComboValue[0] gives you the first character. The selected records should always be accessed using the selected event. But you may store them in a array for later use and overwrite it with any new select.

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

回答by Izhaki

You can also use:

您还可以使用:

var iComboValue = simpleCombo.getValue();

回答by Ahmed MEZRI

may be you should try this

也许你应该试试这个

 // to get the combobox selected item outside the combo listener
        simpleCombo.on('change', function (combo, record, index) {

            alert(record);  // to get the selected item

            console.log(record);  // to get the selected item

        });