admin管理员组

文章数量:1289836

I am trying to execute some code of js when user press key "enter" or "tab". When I alert keycode then showing correct, but when press key "enter or tab" then not showing any response. I tried following code.

<input id="to" onkeypress="checkemail(event);">

function checkemail(evt)
{
    var keycode =  evt.keyCode? evt.keyCode : evt.charCode;
    //alert(keycode);
    if(keycode=='13'|| keycode=='9')
    {
          //MyCode
        }

}

This code is work properly in FireFox, but not showing any result in Google Chrome. Please tell me any solution if have. Thanks in advance.

I am trying to execute some code of js when user press key "enter" or "tab". When I alert keycode then showing correct, but when press key "enter or tab" then not showing any response. I tried following code.

<input id="to" onkeypress="checkemail(event);">

function checkemail(evt)
{
    var keycode =  evt.keyCode? evt.keyCode : evt.charCode;
    //alert(keycode);
    if(keycode=='13'|| keycode=='9')
    {
          //MyCode
        }

}

This code is work properly in FireFox, but not showing any result in Google Chrome. Please tell me any solution if have. Thanks in advance.

Share Improve this question asked Jul 23, 2012 at 14:32 Gokul ShindeGokul Shinde 9653 gold badges10 silver badges30 bronze badges 1
  • 2 Use keyup instead of keypress, keypress is not guaranteed to fire for all keys. – Esailija Commented Jul 23, 2012 at 14:34
Add a ment  | 

5 Answers 5

Reset to default 2
<html>

<body>

<input type = "text" id = "to" onkeydown="check(event)">

<script type = text/javascript>

function check(evt)
{
var keycode = evt.keyCode;
alert(keycode);
}

</script>

</body>

</html>

it works in chrome :)

I remend using jquery's keydown. it will deal with cross browser issues like this for you

function checkemail(evt)
{
    var evt=evt||window.event;
    alert(evt.keyCode);
    if(evt.keyCode==13||evt.keyCode==9)
    {
        //your code
    }
}

I thought my use of keycode in Chrome wasn't working, but it actually does. I used the enter key to trigger opening a new tab in Chrome. With recent Chrome updates, the pop-up blocker prevented me from redirecting in a new tab. The target="_blank" wasn't working & had nothing to do with my condition, event.keycode == 13.

It works in chrome you need to type event.keycode/event.key instead of evt.keycode/evt.keycode.also key press doesn't works for these keys you are using

本文标签: javascriptkeycode not work in chromeStack Overflow