您的位置:首页 > 其它

使用key事件控制输入框输入的范围

2016-08-09 15:45 225 查看
在input输入框输入数字,很多人一开始就想到使用type=“number”,这样只能在输入框中输入数字,但是前者标签元素是h5的新元素。要是使用的不是支持h5标签的浏览器就不好解决了。所以现在使用key事件来解决这个问题。

首先需要在input里面的添加一个class,然后使用下面的代码就可以实现智能输入数字了。

$(".aa").keydown(function(event){

var e = $(this).event || window.event;

var code = parseInt(e.keyCode);

var key =e.key;

var value = $(this).val();

if(value.length==0){

if (code >= 97 && code <= 105 || code >= 49 && code <= 57 && key>=0 && key<= 9 || code == 8) {

return true;

}else {

return false;

}

}else{

if (code >= 96 && code <= 105 || code >= 48 && code <= 57 && key>=0 && key<= 9|| code == 8) {

return true;

}else {

return false;

}

}

});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐