在extjs中使用Textfield上的blur会导致错误

时间:2012-08-28 06:19:13

标签: extjs extjs4

我是extjs的新手。我正在使用extjs-4.1.0,我有一个文本字段,我希望在其blur事件上调用trim方法。我的代码在mozilla firefox上工作正常,但是当文本框失去焦点时,会导致IE上的错误,“对象不支持此属性或方法”。 有没有其他方法来处理IE中的模糊事件?

在下面找到我的代码:

{
        flex:1,
        xtype:'textfield',
        fieldLabel: 'Name',
        allowBlank: false,
        maxLength: 50,
        name: 'name',
        maskRe: /[a-zA-Z\s]+$/,
        validator: function(v) {
            if(!(/[a-zA-Z\s]+$/.test(v))){
                return "This Field should be in alphabets";
            }
            return true;
        },
        listeners: {
             render: function(c) {
                Ext.QuickTips.register({
                target: c.getEl(),
                text: 'Format: John/John Kelvin'
                })  
            },
            blur: function(d) {
                var newVal = d.getValue().trim();
                d.setValue(newVal);

            }
        }
    }

3 个答案:

答案 0 :(得分:4)

我将侦听器放在控制器中,似乎在IE中工作正常。

Ext.define('BM.controller.FormControl', {
    extend: 'Ext.app.Controller',

   views: [
        'MyForm'
    ],

   init: function() {
        if (this.inited) {
            return;
        }
        this.inited = true;
        this.control({
            'my-form > field': {
                blur: this.outOfFocus
            }
        });
    },

    outOfFocus: function(field, event) {
        var newVal = field.getValue().trim();
        field.setValue(newVal);
    }
});

答案 1 :(得分:0)

我的猜测是,这与模糊无关,实际上field.getValue()在某些情况下不是字符串,例如null或numeric或沿着该行的某些东西。尝试替换var newVal = field.getValue().trim();

var newVal = Ext.String.trim(field.getValue());

答案 2 :(得分:0)

当文本字段为空时,浏览器IE 7/8将文本字段值(d.getValue())返回为 NULL ,因此对trim()的方法调用失败,因为没有创建有效对象。

以下解决方案适用于所有浏览器:

this.setValue(Ext.util.Format.trim(this.getValue()));