2010-04-06 84 views
1

我得到了一些JS代碼相關的問題一個小範圍,也許有人能向我解釋什麼,我作出錯誤:JavaScript和ExtJS的 - 範圍的問題

我使用ExtJS的,並得到了這個片斷:

Ext.onReady(function(){ 

// Form for filter selection 
var formFilter = new Ext.FormPanel({ 
    // ... 
     items: [ 
    cbGroup = new Ext.form.ComboBox({  
       fieldLabel: 'Group', 
    store: dsGroups, 
    displayField: 'name', 
    valueField: 'number', 
    emptyText : '- Please choose a group -', 
    listeners:{ 
     'select': function() { 
     alert(cbGroup.selectedIndex +' '+this.selectedIndex); 
     } 
    } 
    }) 
    ] 
    }); 
}); 

問題: 當我通過偵聽器函數中的'this'訪問組合框時,我得到了selectIndex屬性的正確結果。 當我通過它的var名稱訪問組合框時,我總是得到結果'-1'。 非常感謝您的幫助!

回答

0

快速和骯髒的:

Ext.onReady(function(){ 
    var self = this; 
    ... 
    alert(cbGroup.selectedIndex +' '+self.selectedIndex); 
0

嘗試建立對象後添加監聽器。所以:

Ext.onReady(function(){ 
// Group Combobox: 
var cbGroup = {}; 
// Form for filter selection 
var formFilter = new Ext.FormPanel({ 
    // ... 
     items: [ 
    cbGroup = new Ext.form.ComboBox({  
       fieldLabel: 'Group', 
    store: dsGroups, 
    displayField: 'name', 
    valueField: 'number', 
    emptyText : '- Please choose a group -', 
    }) 
    ] 
    }); 
cbGroup.on('select',function() { 
     alert(cbGroup.selectedIndex +' '+this.selectedIndex); 
     }); 
}); 
+0

這也是我的第一個想法(不過我使用var cbGroup而不是VAR cbGroup = {} 不幸的是,似乎並不工作 – ben 2010-04-06 13:48:26

+0

OK現在編輯我的答案試過了......。 – Josh 2010-04-06 15:11:17