【问题标题】:How to get the value of checkbox如何获取复选框的值
【发布时间】:2011-08-15 21:15:07
【问题描述】:

如何获取复选框的值?

var tb = new Ext.Toolbar();

tb.add({
                xtype: 'checkbox',
                boxLabel: 'Expand Groups by Default',
                id: 'GetChkBoxValue',
                checked: true,
                handler: function() {
                    alert(this.getValue())
                }
       });

是否可以在 tb 之外获取复选框的值。我做过类似的事情,但它没有触发

Ext.getCmp('GetChkBoxValue').getValue(); 

【问题讨论】:

  • 发生了什么?一个错误?您创建的代码应该可以工作。我唯一能想到的是,您试图在呈现工具栏之前调用 getValue 。由于您的复选框是按需创建的(通过传入 cfg 对象),因此 Ext.getCmp 在呈现之前不会找到该元素。
  • 是的,问题是它总是给我错误,因为对象已实例化并在渲染之前被调用。我想知道如何获取复选框的值??
  • 我可以访问网格面板中复选框的值,如下所示 Ext.getCmp('chkid').getValue() 如果它是真的,我必须使用这个值,如果它是错误的,我必须折叠组。谁能帮我展开和折叠网格面板中的组。

标签: javascript extjs


【解决方案1】:

试试下面的代码,它应该可以工作:

new Ext.Window({
    renderTo: Ext.getBody(),
    width: 500,
    height: 200,
    title: 'test window',
    items: [{
       xtype: 'checkbox',
       boxLabel: 'Expand Groups by Default',
       id: 'chkid',
       checked: true
    }]
}).show()
Ext.getCmp('chkid').getValue()

然后使用复选框并使用 getValue() 获取其状态(选中或未选中)。祝 ExtJS 编码愉快!

【讨论】:

    【解决方案2】:

    这对我有用:

    var expandAllGroupsCheckbox = Ext.create(
    
                {
                    xtype: 'checkbox',
                    boxLabel: 'Expand Groups by Default',
                    id: 'chkid',
                    checked: true,
                    afterRender: function() {
                        Ext.form.Checkbox.superclass.afterRender.call(this);
                        alert(this.getValue());// giving true 
                        this.checkChanged();
                    },
                    checkChanged: function()
                    {
                        var checked = expandAllGroupsCheckbox.getValue();
                        gv.startCollapsed = !checked;
                        if ( gv.mainBody )
                        {
                            gv.toggleAllGroups( !checked );
                        }
                    },
                    listeners: {
                        check: {
                            fn: function(){expandAllGroupsCheckbox.checkChanged()}
                        }
                    }
    
                });
    

    然后:

    tbar: [
                   expandAllGroupsCheckbox
    ,
                        {
                            xtype: 'tbbutton',
                            icon: '/images/icon_list_props.gif',
                            handler: function() {
                                ShowPreferencesPage();
                            }
                        }
    ],
    

    【讨论】:

      【解决方案3】:

      就这个:

      var cbox = Ext.getCmp('cboxId');
      alert(cbox.checked);
      

      【讨论】:

        猜你喜欢
        • 2021-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多