国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jquery限定文本框只能輸入數(shù)字(整數(shù)和小數(shù))

2019-11-20 10:50:25
字體:
供稿:網(wǎng)友

本文實(shí)例介紹了jquery限定文本框只能輸入數(shù)字的詳細(xì)代碼,分享給大家供大家參考,具體內(nèi)容如下

先來一段規(guī)定文本框只能夠輸入數(shù)字包括小數(shù)的jQuery代碼:

<!DOCTYPE html> <html> <head> <meta charset="gb2312"> <title>武林網(wǎng)</title> <script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script><script type="text/javascript">//文本框只能輸入數(shù)字(包括小數(shù)),并屏蔽輸入法和粘貼 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>

2、jQuery如何規(guī)定文本框只能輸入整數(shù):
有時(shí)候文本框的內(nèi)容只能夠是數(shù)字,并且還只能夠是整數(shù),例如年齡,你不能夠填寫20.8歲,下面就通過代碼實(shí)例介紹一下如何實(shí)現(xiàn)此功能,希望給需要的朋友帶來幫助,代碼如下:

<html> <head> <meta charset="gb2312"> <title>螞蟻部落</title> <script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script><script type="text/javascript">//文本框只能輸入數(shù)字(不包括小數(shù)),并屏蔽輸入法和粘貼 jQuery.fn.integer=function(){  this.bind("keypress",function(e){  var code=(e.keyCode?e.keyCode:e.which); //兼容火狐 IE  //火狐下不能使用退格鍵  if(!$.browser.msie&&(e.keyCode==0x8)) {   return ;  }  return 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>

以上代碼實(shí)現(xiàn)了我們的要求,文本框中只能夠輸入整數(shù)。

希望本文所述對(duì)大家學(xué)習(xí)jquery程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 石泉县| 芷江| 盐池县| 清原| 河北区| 阿克苏市| 五河县| 黄平县| 太保市| 塔城市| 洛扎县| 施秉县| 塔河县| 尚义县| 定远县| 隆子县| 呈贡县| 赫章县| 拉萨市| 博罗县| 宁晋县| 绥江县| 南阳市| 靖边县| 洛川县| 淳安县| 德昌县| 罗城| 陈巴尔虎旗| 南郑县| 鞍山市| 星子县| 新和县| 高陵县| 西畴县| 关岭| 诸暨市| 刚察县| 临武县| 含山县| 峡江县|