2010-04-06 81 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