【问题标题】:In extjs4 How to catch textarea's keypress event in controller在 extjs 4 中如何在控制器中捕获 textarea 按键事件
【发布时间】:2013-04-09 07:08:35
【问题描述】:

我在 extjs 工作。我有一个文本区域来输入单词和一个搜索按钮。我有视图-

Ext.define('Balaee.view.kp.Word.Word', {
    extend : 'Ext.form.Panel',
    id : 'WordId',
    alias : 'widget.Word',
    title : 'Dictionary',
    height : 500,
    items : [{

        xtype : 'textfield',
        fieldLabel : 'Enter the word',
        name : 'wordtext',
        //anchor:'100%',
        allowBlank : false,
        emptyText : 'Enter the word',
        enableKeyEvents : true,
        id : 'wordtext',
        action : 'EnterAction'
        }, 
        {
        xtype : 'button',
        formBind : true,
        fieldLabel : 'Search',
        action : 'SearchAction',
        text : 'Search'
        }
    ]
    });

在控制器中我的功能是-

    SearchWord:function(button)
    {

        var j = Ext.getCmp('wordtext').getValue();
        console.log("word is:"+j);

        var answers = '{"data":[';
        answers = answers + '{"word":"'+j+'"}'
        answers =answers+']}';
        console.log(answers);

        var storeObject=this.getStore('kp.WordStore');
        storeObject.load({

            params:{
                data: answers 
            },
            callback: function(records,operation,success){
                //console.log(records)
            },
            scope:this
        });
        var temp=Ext.getCmp('WordId');
        temp.remove('WordInfoId');
        var details=Ext.create('Balaee.view.kp.Word.WordInfo');
        //details.region='center';
        temp.add(details);
    }
});

现在,当用户在 textarea 中输入单词并单击提交按钮时,上述功能可以正常工作。但我也想在输入按钮单击时执行控制器的“SearchWord”功能。即如果用户将在 textarea 中输入单词并按下 enter 键,则需要执行控制器的相同功能。那么如何在控制器中捕获 textarea 的 enter key press 事件?

【问题讨论】:

标签: event-handling extjs4 textarea enter


【解决方案1】:

只需监听specialKey 事件并在键为 ENTER 时调用您的函数。

所以在你的控制器的初始化函数中,做这样的事情:

this.control({
    '#wordtext':{
        specialkey: function(field, e){
            if (e.getKey() == e.ENTER) {
                 //Do whatever you want here (such as call your SearchWord function)
            }
        }  
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多