admin管理员组文章数量:1321615
I have written a javascript on a button which blocks the keypress or keydown event on that control. It works fine with key like Enter but when I press space bar key it event gets fired.
Is there any solution for the same?
I have written a javascript on a button which blocks the keypress or keydown event on that control. It works fine with key like Enter but when I press space bar key it event gets fired.
Is there any solution for the same?
Share Improve this question edited May 26, 2014 at 8:02 brasofilo 26.1k15 gold badges93 silver badges186 bronze badges asked Jun 7, 2010 at 9:47 A Bright WorkerA Bright Worker 1,31814 silver badges21 bronze badges2 Answers
Reset to default 6Make sure you're using the correct ascii character code for the keypress event, and make sure you're checking the correct properties for the browser you're testing in. IE uses event.keyCode
, others use event.which
. The following code works fine for me in IE, Firefox and Chrome:
document.getElementById("hello").onkeypress = function (evt)
{
var k = evt ? evt.which : window.event.keyCode;
if (k == 32)
return false;
}
http://jsbin./ikowo4
d actually if you pare keycode 32 its little different. I could resolve this by .keydown and keycode = 32... – A Bright Worker Jun 7 '10 at 10:04
@A Bright Worker: Did you take a look at the jsbin link I provided? It has an input box with the code I used here working just fine in IE, Firefox and Chrome. It will allow any character to be input except for the space character. Beyond that, I'm not sure I understand what your ment is trying to say. – Andy E Jun 7 '10 at 10:07
@Andy: Actually the type of input is a filter button in a grid and which already has some onclick event, so if I use onkeypress the event still gets fired. but if I use onkeydown the onclick event does not. Thanks for your example.. I have modified a bit, you can check it out. jsbin./ikowo4/2 – A Bright Worker Jun 7 '10 at 10:38
@A Bright Worker: ahh, that makes it a bit clearer now. Take a look at jsbin./ikowo4/4, with your developer tools (Firebug, IE Dev Tools, Chrome Dev Tools, etc) console open. Each event has return false, so the next event in the chain should not fire. This is true in Chrome and IE, which show only the onkeydown event firing, whereas Firefox shows all three events firing. I'm not sure if this even helps you, but it looks like you will have to find out exactly what the differences in each browser before you get closer to your answer. Sorry I couldn't help further, good luck. – Andy E Jun 7 '10 at 11:11
Thanks Andy, Infact my application supports on IE so I do not have problem currently. – A Bright Worker Jun 10 '10 at 7:01
本文标签: javascriptonkeypress return false does not work for space barStack Overflow
版权声明:本文标题:javascript - onkeypress return false does not work for space bar - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742103140a2420895.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论