Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript和extJs-范围问题_Javascript - Fatal编程技术网

javascript和extJs-范围问题

javascript和extJs-范围问题,javascript,Javascript,我在一些js代码中遇到了一个与范围相关的小问题,也许有人可以向我解释我做错了什么: 我正在使用extJs,得到了以下代码片段: Ext.onReady(function(){ // Form for filter selection var formFilter = new Ext.FormPanel({ // ... items: [ cbGroup = new Ext.form.ComboBox({ fieldLabel

我在一些js代码中遇到了一个与范围相关的小问题,也许有人可以向我解释我做错了什么:

我正在使用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属性的正确结果。 当我通过组合框的变量名访问它时,我总是得到结果'-1'。 非常感谢你的帮助

又快又脏:

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

设置对象后,尝试添加侦听器。因此:

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

这也是我的第一个想法(但我尝试使用var cbGroup而不是var cbGroup={}),不幸的是,两者似乎都不起作用。