在TextField上使用setValue的正确方法是什么?

时间:2011-12-16 23:52:52

标签: javascript extjs

我有一个似乎是一个简单的问题,但我在这一点上很难过。我正在研究一些基于Ext.JS的UI代码,我想更改表单字段中某些文本的值。

该字段是ext.js.TextField。

我有这样的代码:

var foo = this.getForm().findField('myFooField');
console.log(foo);
foo.setValue("text different that is different from the default");

如果我运行此代码,“foo”肯定会被记录到控制台,它是一个正确的对象,填充了我期望的值。但是,对setValue的调用似乎没有做任何事情。

我在setValue之前和之后放了一些跟踪调用,以确保它确实运行,并且一切似乎都没有问题。这只是UI没有反映我的变化。我也尝试过调用“setRawValue”,但没有区别。

有什么建议吗?非常感谢!

3 个答案:

答案 0 :(得分:2)

如果您正在使用MVC,可能是在尝试更改窗口渲染事件的值,对于文本字段,设置值仅适用于afterRender事件。

答案 1 :(得分:1)

您的代码看起来正确,但我通常使用像这样的函数

Ext.override(Ext.Container, {
setValue: function(c, v) {this.findById(c).setValue(v);},
getValue: function(c) {return this.findById(c).getValue();}
}); 
win.setValue('myFooField', 'Some text');  

答案 2 :(得分:1)

我不确定为什么你的代码不起作用。检查以下代码是否有效。

Ext.getCmp('myFooField').setValue("text different that is different from the default");

即使这不起作用,可能你的代码也在错误的位置。