2009-12-21 104 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);