星空网 > 软件开发 > Java

规定文本框只能够输入数字包括小数的jQuery代码

规定文本框只能够输入数字包括小数的jQuery代码:
文本框有时候规定只能够输入整数,这里就不多介绍了,具体可以参阅jQuery如何规定文本框只能输入整数一章节,不过有时候也可以输入小数,下面就通过代码实例介绍一下如何实现此功能。
代码实例如下:

 

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>蚂蚁部落</title> <script type="text/javascript" src='/images/loading.gif' data-original="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<= 57}  });  this.bind("paste",function(){return false;});  this.bind("keyup",function(){  if(this.value.slice(0,1) == ".")  {    this.value = "";   }  });  this.bind("blur",function(){   if(this.value.slice(-1) == ".")  {    this.value = this.value.slice(0,this.value.length-1);   }  }); }; $(function(){  $("#txt").number();}); </script></head><body><input type="text" id="txt" /></body></html>

 

以上代码实现了我们的要求,文本框中能够输入数字,包括整数和小数。

原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=10801

更多内容可以参阅:http://www.softwhy.com/jquery/

 




原标题:规定文本框只能够输入数字包括小数的jQuery代码

关键词:jquery

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

除了价格:https://www.goluckyvip.com/tag/1121.html
小家电:https://www.goluckyvip.com/tag/11210.html
主机服务器:https://www.goluckyvip.com/tag/11211.html
跨境圈大小事:https://www.goluckyvip.com/tag/11212.html
玩具电商:https://www.goluckyvip.com/tag/11213.html
唐朝国际物流:https://www.goluckyvip.com/tag/11214.html
TikTok斥资210万美元游说美国参议院阻止法案通过 :https://www.goluckyvip.com/news/188220.html
北京飞机票查询(快速查询北京至各地机票价格和航班信息):https://www.vstour.cn/a/366178.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流