开发者

Make Up/Down arrow in input boxes not do anything

开发者 https://www.devze.com 2023-04-08 15:22 出处:网络
How can I make <input> elements not react to pressing the Up arrow (keyCode 38) or the Down arrow (keyCode 40), while they are focus开发者_StackOverflowed? I\'m using jQuery for the project, but

How can I make <input> elements not react to pressing the Up arrow (keyCode 38) or the Down arrow (keyCode 40), while they are focus开发者_StackOverflowed? I'm using jQuery for the project, but have no qualms against writing it in raw JS if that's easier.


Like this:

$('.yourinputclass').keypress(function(e) {
    if(e.which == 38 or e.which == 40) return false; // or you can use e.preventDefault(); like it was mentioned in the comments
});

Documentation here

0

精彩评论

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