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
5 Answers
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
版权声明:本文标题:javascript - keycode not work in chrome - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741445505a2379173.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论