比较if语句中的两个键码条件--javascript [英] Comparing two keycode conditions in if statement - javascript
本文介绍了比较if语句中的两个键码条件--javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望输入不接受数字,所以键码从48到57.
我不知道下面的代码有什么问题:
谢谢。
注意:在Chrome和Edge中测试过。
编辑小提琴 - JSFiddle [ ^ ]
我的尝试:
I want the input does not accept digits, so the key codes for that are from 48 to 57.
I don't know what is the problem in the code below:
Thanks.
Note: Tested in Chrome and Edge.
Edit fiddle - JSFiddle[^]
What I have tried:
<p></p>
<input type="text" id="in">
$("#in").on('keydown', function(evt) {
var theKeyCode = (evt.keyCode ? evt.keyCode : evt.which);
if (theKeyCode == 32) return false; //if spacebar (works)
// if digits (does not works)
if (theKeyCode >= 48 && theKeyCode <= 57) {
$("p").html(theKeyCode);
return false;
}
});
推荐答案
( #in)。on(' keydown' , function (evt){
var theKeyCode =(evt.keyCode? evt.keyCode:evt.which);
if (theKeyCode == 32 ) return false ; // < span class =code-comment> if spacebar(works)
// 如果数字(不起作用)
if (theKeyCode> = 48 && theKeyCode< = 57 ){
("#in").on('keydown', function(evt) { var theKeyCode = (evt.keyCode ? evt.keyCode : evt.which); if (theKeyCode == 32) return false; //if spacebar (works) // if digits (does not works) if (theKeyCode >= 48 && theKeyCode <= 57) {
( P跨度>)HTML(theKeyCode);
return false ;
}
});
("p").html(theKeyCode); return false; } });
Quote:
我希望输入不接受数字,因此密钥代码从48到57。
I want the input does not accept digits, so the key codes for that are from 48 to 57.
此代码
This code
if (theKeyCode >= 48 && theKeyCode <= 57) {
仅适用于数字
且这只适用于非数字
works only for digits
and this one only for non digits
if (theKeyCode < 48 || theKeyCode > 57) {
这篇关于比较if语句中的两个键码条件--javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文