如何捕获ENTER键是$ P $在文本框pssed? [英] How to Capture ENTER Key which is pressed in TextBox?

查看:238
本文介绍了如何捕获ENTER键是$ P $在文本框pssed?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个简单的聊天系统,ASP.NET使用C#(它的一个网站,而不是Windows应用程序)。我显示,其中包含一个文本框,使用户可以在写他/她的消息,聊天窗口。我要的是,当用户presses ENTER键,那么我将能够捕捉ENTER pressed事件,并可以调用,这将是在cs文件的功能。我怎样才能做到这一点?这是我的项目code段:

I am developing a simple chat system in ASP.NET with C# (Its a website not windows application). I am showing a chat window which contains a TextBox so that user can write his/her message in it. What i want is that when user presses ENTER key then I will be able to capture that ENTER pressed event and could call a function which will be in .cs file. How can i do that? This is the code segment from my project:

    <!-- Some code here -->

    <asp:textbox TextMode="Multiline"
    Columns="28"
    id="Textbox1"
    Rows="5"
    runat="server"
    BorderStyle="Solid"
    BorderColor="Bisque"
    class="msg-area">
    </asp:textbox>

</asp:Panel>

感谢您。

推荐答案

执行快速通过jQuery捕获文本框:

Quick implementation of capturing the textbox via jQuery:

$.fn.captureEnter = function(options) {
// Parameters = OBJECT {action, preventDefault}
// action - Javascript function to be called upon the enter keypress. Send NULL to perform NO ACTION
// preventDefault - Set to FALSE if you don't want to return FALSE
var defaults = {
    action: function() {
        alert('The enter button has been captured. Please provide a function to run on enter.');
    },
    preventDefault: true
};
options = $.extend(defaults, options);

return this.each(function() {
    var el = this;
    $(el).keypress(function(e) {
        var code = e.keyCode || e.which;
        if (code == 13) { //Enter keycode
            if ((typeof options.action) == "function") {
                options.action(el);
            }

            if (options.preventDefault) {
                e.preventDefault();
            }

        }
    });
});

};

$('TEXTBOX').captureEnter({
    action: postMessageAjaxFunction
});

这篇关于如何捕获ENTER键是$ P $在文本框pssed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆