如何获取复选框的值

时间:2009-12-21 17:29:35

标签: javascript extjs

如何获取复选框的值?

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

3 个答案:

答案 0 :(得分:3)

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

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编码!

答案 1 :(得分: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();
                        }
                    }
],

答案 2 :(得分:1)

就是这样:

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