2013-03-01 125 views
3

我使用ExtJS的3,我需要啓用/禁用特定文本字段,當我選擇/取消選中複選框,就像下面的例子表明:的複選框選擇啓用/禁用文本字段ExtJS的

{ 
    fieldLabel: 'myCheckBox' 
    xtype: 'checkbox', 
    name: 'myCheckBox' 
},{ 
    fieldLabel: 'myTextField' 
    xtype: 'textfield', 
    name: 'myTextField', 
    disabled: true 
} 

據我所知,我在「myCheckBox」使用監聽器是這樣的:

listeners: { 
    change: function() { 

    } 
} 

但我不知道是什麼參數傳遞給我的功能以及如何定位「myTextField」和.enable().disable()它。你能幫我麼?非常感謝你。基於答案


解決方案(謝謝):

listeners: { 
    change: function(cb, checked) { 
     Ext.getCmp('myTextField').setDisabled(!checked); 
} 
} 

,並添加了ID標籤的文本字段組件這樣的:

id: 'myTextField' 
+0

謝謝Mark Peters和Ankit的正確答案。我已經在解決方案中使用了它們,我已經將其添加到其他人的初始問題中,並且具有相同的疑問 – mikethe 2013-03-01 17:58:23

回答

4

如果您不確定要傳遞什麼參數而不是Ext.getCmp()爲您提供組件。它將組件的id作爲參數。在你的情況下,你必須將id分配給textfield,並且可以將它作爲Ext.getCmp('myTextField')在change事件上獲取。其中myTextField是文本字段的ID。組件的名稱和Id可以相同。

listeners: { 
change: function() { 
    Ext.getCmp('myTextField').disable(); 
    //or 
    Ext.getCmp('myTextField').enable(); 
} 
} 
+0

謝謝Ankit – mikethe 2013-03-01 18:05:12

1

有參考myTextField幾種方法。最簡單的是可能給該領域的ref(請注意,這種方法並不能在ExtJS的4,你在哪裏與組件查詢更好的工作):

{ 
    fieldLabel: 'myTextField' 
    xtype: 'textfield', 
    name: 'myTextField', 
    ref: '../myTextField' 
    disabled: true 
} 

設置裁判將導致文本框組件是在其所有者或其祖先的財產中引用。所以,那麼你的聽衆可以簡單地是這樣的(傳遞給這個函數的參數是listed in the doc):

change: function(cb, checked) { 
    me.myTextField.setDisabled(!checked); 
} 

你可能需要調整ref路徑取決於您的組件層次。

+0

謝謝Mark Peters – mikethe 2013-03-01 18:05:36

1

實施例:

  1. 使用setDisabled API: theStudentForm.findField( 'stud_name')setDisabled(真)。

  2. 使用setOpacity API: theStudentForm.findField( 'stud_name')labelEl.setOpacity(1);

  3. 使用只讀API: theStudentForm.findField( 'stud_name')setReadOnly(真)。

相關問題