代码之家  ›  专栏  ›  技术社区  ›  harunB10

如何删除ExtJS中禁用的属性

  •  0
  • harunB10  · 技术社区  · 7 年前

    我有一个默认设置为禁用的字段。但我想在单击按钮时更改这个属性(启用它)。

    {
        margin: '10 50 10 50',
        padding: '10 20 10 20',
        xtype: 'textfield',
        name: 'name',
        fieldLabel: 'Survey Name',
        allowBlank: false,
        disabled: true,
        id: 'name'
    },
    

    这是按钮:

    {
        margin: '0 50 0 50',
        padding: '10 20 10 20',
        xtype: 'button',
        text: "Create",
        listeners: {
            click: function() {
                Ext.get('name').disabled = false;
            }
        }
    }
    

    当我点击这个时,什么都没有发生。这里怎么了?

    1 回复  |  直到 6 年前
        1
  •  1
  •   halfer    6 年前

    正如你所提供的 id 到你的组件,所以不要经过 Ext.get() 使用 Ext.getCmp()

    例子

    Ext.getCmp('name').setDisabled(false)
    

    在这个里面 Fiddle ,我用相同的代码创建了一个演示。

    代码段:

    Ext.application({
        name: 'Fiddle',
    
        launch: function () {
            Ext.create({
                xtype: 'panel',
                title: 'Demo',
                renderTo: Ext.getBody(),
                layout: 'hbox',
                bodyPadding: 10,
                items: [{
                    xtype: 'textfield',
                    name: 'name',
                    fieldLabel: 'Survey Name',
                    allowBlank: false,
                    disabled: true,
                    id: 'name'
                }, {
                    xtype: 'button',
                    text: "Create",
                    listeners: {
                        click: function () {
                            Ext.getCmp('name').setDisabled(false);
                        }
                    }
                }]
            })
        }
    });
    

    注意:不要使用 身份证件 使用 itemId 或者extjs组件的任何其他配置,因为id不能重复。所以你也可以这样做

    代码段:

    Ext.create({
        xtype: 'panel',
        title: 'Enable component using ITEM ID',
        renderTo: Ext.getBody(),
        layout: 'hbox',
        bodyPadding: 10,
        items: [{
            xtype: 'textfield',
            name: 'name',
            fieldLabel: 'Survey Name',
            allowBlank: false,
            disabled: true,
            itemId: 'name'
        }, {
            xtype: 'button',
            text: "Create",
            handler: function() {
                this.up('panel').down('#name').setDisabled(false);
            }
        }]
    })