这次给大家带来jquery实现指定文本框只能输入数字,jquery实现指定文本框只能输入数字的注意事项有哪些,下面就是实战案例,一起来看一下。
先来一段规定文本框只能够输入数字包括小数的jquery代码:
<!doctype html>
<html>
<head>
<meta charset="gb2312">
<title>php</title>
<script type="text/javascript" src="mytest/jquery/jquery-1.8.3.js"></script>
<script type="text/javascript">
//文本框只能输入数字(包括小数),并屏蔽输入法和粘贴
jquery.fn.number=function(){
this.bind(keypress,function(e){
var code=(e.keycode?e.keycode:e.which); //兼容火狐 ie
//火狐下不能使用退格键
if(!$.browser.msie&&(e.keycode==0x8)){return;}
if(this.value.indexof(.)==-1){return (code >= 48 && code<= 57)||(code==46);}
else{return code >= 48 && code= 48 && code<= 57;
});
this.bind(paste,function(){
return false;
});
this.bind(keyup,function(){
if(/(^0+)/.test(this.value))
{
this.value = this.value.replace(/^0*/,'');
}
});
};
$(function(){
$(#txt).integer();
});
</script>
</head>
<body>
<input type="text" id="txt" />
</body>
</html>
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
jquery+mobile自定义按钮图标步骤详解
设置多行文本框[textarea]自动生成高度
以上就是jquery实现指定文本框只能输入数字的详细内容。