2009-12-21 102 views
2

如何获取复选框的值?如何获取复选框的值

var tb = new Ext.Toolbar(); 

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

是否有可能得到tb.I外的复选框的值已经做了这样的事情,但不点火

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

这是怎么回事?一个错误?你创建的代码应该可以工作。我能想到的唯一的事情是,在呈现工具栏之前,您正尝试调用getValue。由于您的复选框是按需创建的(通过传入cfg对象),Ext.getCmp在渲染之前不会找到该元素。 – 2009-12-21 19:35:02

+0

是的问题是它总是让我错误,因为对象被实例化,并且在它被渲染之前被调用。我想知道我怎样才能得到一个复选框的值? – xrx215 2009-12-21 21:51:17

+0

我可以访问网格面板中的复选框的值如下 Ext.getCmp('chkid')。getValue() 使用此值如果它是true我必须exapnd组,如果它是假的我不得不垮台。 任何人都可以帮助我扩大和折叠网格面板中的组。 – xrx215 2009-12-21 23:39:53

回答

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

尝试一下下面的代码,它应该工作:

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

就在:

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