2012-01-30 45 views
0

對sencha touch有所觸動。我已經在網上查了很多教程。我有一個問題試圖獲得文本字段的值。我點擊登錄按鈕時發生的錯誤是Uncaught TypeError: Cannot call method 'getValue' of undefined。這是否意味着我的Ext.getCmp未定義呢?我有這個面板包裹在常規Ext.setup....onReady:.....在Sencha Touch中未定義的getValue

var login = new Ext.Panel({ 
    height:'auto', 
    scroll:'vertical', 
    layout:{ 
     type:'vbox', 
     align:'center' 
    }, 
    items:[ 
     { 
      cls:'launchscreen', 
      html:logo, 
      padding:10 
     }, 
     new Ext.form.FormPanel({ 
      width:300, 
      cls:'loginform', 
      items:[ 
       { 
        xtype: 'fieldset', 
        title: 'Login', 
        items: [ 
         { 
          xtype: 'textfield', 
          name : 'username', 
          label: 'Username', 
          labelWidth: 85 
         }, 
         { 
          xtype: 'passwordfield', 
          name : 'password', 
          label: 'Password', 
          labelWidth: 85 
         } 
        ] 
       }, 
       { 
        xtype: 'button', 
        text: 'Submit', 
        ui: 'confirm', 
        handler:function() 
        { 
         alert(Ext.getCmp('username').getValue()); 
        } 
       } 
      ] 
     }) 
    ] 
}); 

編輯:我能得到的值,如果我設置的文本字段中id屬性。我已經看到一些示例,其中id未設置,它們基於name屬性獲取值。所以我想現在我的問題是我應該得到基於idname的價值?

回答

1

使用Ext.getCmp()你必須提供你想要的值的元素ID。見煎茶API文檔的這個:

getCmp(字符串ID) 這是縮略表示Ext.ComponentManager.get。通過ID 參數 查找現有的組件ID:字符串 組件ID

您也可以找到名稱的元素,但我認爲這是由ID更快,但或許也爲瀏覽器的引擎有點貴。真的不能說任何事情。

無論如何,通過使用findField()方法可以按名稱查找字段。使用此方法,您應提供所需字段的ID或名稱。見API文檔:http://docs.sencha.com/ext-js/4-0/#!/api/Ext.form.Basic-method-findField

例子:

var fieldValue= Ext.getCmp("YourFormID").getForm().findField("FieldName").getValue(); 
+0

是的,我想傳遞ID是更好的結局。謝謝(你的)信息。我無法在他們的文檔中找到getCmp方法 – Ronnie 2012-01-30 21:48:38