【问题标题】:get checkbox value without using Ext.getCmp在不使用 Ext.getCmp 的情况下获取复选框值
【发布时间】:2013-03-12 17:58:25
【问题描述】:

我正在尝试获取此复选框的值

 Ext.define('myRoot.myExtApp.myForm', {
    extend: 'Ext.form.Panel',
    layout: {
      type: 'vbox',
      align: 'stretch'
    },
    scope: this,
    constructor: function() {
       this.callParent(arguments);

       this.myFieldSet = Ext.create('Ext.form.FieldSet', {
         scope: this,
         columnWidth: 0.5,
         collapsible: false,
         defaultType: 'textfield',
         layout: {
             type: 'hbox', align: 'stretch'
         }
       });

       this.mySecondForm = Ext.create('myRoot.myExtApp.myForm2', {
         scope: this,
         listener: this,
         margin: '1 3 0 0'
       });
       this.myCheckBox = Ext.create('Ext.form.Checkbox', {
           scope: this,
           //id: 'myCheckBox',
           boxLabel: 'Active',
           name: 'Active',
           checked: true,
           horizontal: true
       });

       this.myFieldSet.add(this.mySecondForm);
       this.myFieldSet.add(this.myCheckBox);

       this.add(this.myFieldSet);
     }
 });

如你所见,我有另一种形式

Ext.define('myRoot.myExtApp.myForm2', {

我有一个处理程序,它应该从“myForm”获取复选框的值

如何在不使用 Ext.getCmp 的情况下从 Form2 中获取复选框的值?我知道如果我这样做,我可以获得复选框的值

Ext.getCmp('myCheckBox').getValue();

但使用

this.myCheckBox.getValue();

给我未定义的错误。

更新 - 根据 Wared 的建议,我在 myForm2 中尝试了这个

this.temp=Ext.create('myRoot.myExtApp.myForm'), {});
var tempV = this.temp.myCheckBox.getValue();

我能够获得价值,但即使我取消选中该框,我也会获得相同的真实价值

【问题讨论】:

  • 你的结构发布的不够多,我们需要看看它们是如何组合在一起的。
  • 延伸 Evan 的回应——在this.myCheckBox 中无法分辨this 的范围。
  • @EvanTrimboli,感谢您花时间查看我的帖子。请看我的更新。我已经用我的完整结构对其进行了编辑。
  • @Izhaki...嗨,Izhaki,请参阅我更新的帖子。这会有帮助吗?
  • 你能创建一个jsFiddle吗?这真的很有帮助。

标签: javascript scope extjs4.1


【解决方案1】:

我假设您担心过度使用组件查询会导致性能损失。最小化组件查询的一个好技巧是在闭包内定义一个新方法,以便缓存第一个 getCmp 调用的结果。将方法的定义包装在闭包中可以避免使用全局范围或无用的类属性。

getMyCmp: function (cmp) { 
    // "cmp" does not exist outside this function
    return function () { 
        return cmp = cmp || Ext.getCmp('#myCmp'); 
    }; 
}()

【讨论】:

    【解决方案2】:

    一种解决方案可能是:

    myRoot.myExtApp.myForm.myCheckBox.getValue();
    

    小心,错误的答案。请参阅下面的 cmets 以获得有效的解决方案。

    【讨论】:

    • @wared...谢谢...但我仍然收到未定义的错误。我尝试的另一件事是在 myForm2 上...我做了 this.temp=Ext.create('myRoot.myExtApp.myForm'), {});和 var tempV = this.temp.myCheckBox.getValue();... 我得到了值,但即使我取消选中该框,我也会得到相同的真实值
    • 对不起,我完全错了......“myRoot.myExtApp.myForm”指的是类,而我们需要一个实例。我需要更多细节来帮助你。实例化表单的代码如何?
    • 感谢您...您部分正确。我必须确保我声明了父级:在我的扩展类中,我所要做的就是...... this.parent.myCheckBox.getValue();
    猜你喜欢
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    • 2014-10-13
    • 2020-02-24
    相关资源
    最近更新 更多