开发者

how to catch key event on textbox?

开发者 https://www.devze.com 2023-01-27 21:05 出处:网络
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:

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

精彩评论

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