【问题标题】:How to use the onkeyup event to a TextField?如何对 TextField 使用 onkeyup 事件?
【发布时间】:2018-07-03 17:29:31
【问题描述】:

我想使用文本字段的onkeyup事件,这样我们可以在输入文本时计算那个字符。

【问题讨论】:

  • 听起来你已经知道如何解决这个问题了。当您在实施时遇到困难,请告诉我们。
  • 我尝试了以下方法,但 onkeyup 事件似乎没有为 TextField 对象触发:- new Ext.form.TextField({ emptyText:'insert name', listeners: { ' onkeyup': function(obj) { alert('test fire'); } } }),
  • 你有什么解决办法吗?

标签: extjs extjs3


【解决方案1】:

你快到了。在文档field key events 中,只有在 enableKeyEvents 设置为 true 时才会触发键事件。

           {
                xtype: 'textfield',
                fieldLabel: 'Address',
                width: '100%',                    
                enableKeyEvents : true,
                listeners: { 
                    keyup : function(obj) { 
                     alert('test fire ' + obj.getValue()); 
                     } 
                   }
            },

【讨论】:

  • 感谢活动正常运行!现在我们可以在函数'Obj'中获取我们在文本字段中输入的文本字符,以便我们可以计算字符的长度吗?
  • 使用文本字段对象的 getValue() 方法:- obj.getValue()。看看上面修改后的代码
【解决方案2】:
enableKeyEvents: true

要使关键事件起作用,您需要明确指定它。

工作 POC 代码:

Ext.onReady(function () {
    Ext.create({
        xtype: 'panel',
        renderTo: Ext.getBody(),
        items: [{
            xtype: 'form',
            height: 100,
            items: [{
                xtype: 'textfield',
                fieldLabel: 'field label',
                value: '1',
                enableKeyEvents: true,
                listeners: {
                    keyup: function() {
                        console.log("key Up called");
                    },
                    change: function() {
                        console.log('changed');
                    }
                }
            }]
        }]
    })
});

工作小提琴:https://fiddle.sencha.com/#view/editor&fiddle/2cbd

【讨论】:

  • 感谢活动正常运行!现在我们可以在函数'Obj'中获取我们在文本字段中输入的文本字符,以便我们可以计算字符的长度吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多