开发者

how to catch key event on textbox?

if i have focus on textbox and i press enter or开发者_如何学Go Esc

how to catch this event ?


You can use the JavaScript onkeyup event directly:

<input id="Text1" type="text" onkeyup="keyPress(event)" />

Ore use the C# alternative:

Text1.Attributes.Add("onkeyup", "keyPress(this)");

Then the script code:

<script type="text/javascript">
    function keyPress(e)
    {
        var textBox = document.getElementById('Text1');

        var keynum;

        if (window.event) // IE
            keynum = e.keyCode;
        if (e.which) // Other browser
            keynum = e.which;

        switch (keynum)
        {
            case 13:
                //enter key
                break;
            case 27:
                //esc
                break;
        }
    }
</script>

Here there are some guidelines to catch the keystrokes in JavaScript, with a sample at the bottom.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜