jQuery實(shí)現(xiàn)只允許輸入數(shù)字和小數(shù)點(diǎn)的方法
來源:易賢網(wǎng) 閱讀:1471 次 日期:2016-08-02 16:26:25
溫馨提示:易賢網(wǎng)小編為您整理了“jQuery實(shí)現(xiàn)只允許輸入數(shù)字和小數(shù)點(diǎn)的方法”,方便廣大網(wǎng)友查閱!

本文實(shí)例講述了jQuery實(shí)現(xiàn)只允許輸入數(shù)字和小數(shù)點(diǎn)的方法。分享給大家供大家參考,具體如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<script src="jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">

//示例代碼:

//只允許輸入數(shù)字與.:<input type="text" name="test" id="test" onkeydown="checkKeyForFloat(this.value,event)" style="ime-mode: disabled" />

//只允許輸入數(shù)字 :<input type="text" name="test2" id="test2" onkeydown="checkKeyForNum(this.value,event)" style="ime-mode: disabled" />

//只允許輸入數(shù)字與小數(shù)點(diǎn)

function checkKeyForFloat(value, e) {

 var isOK = false;

 var key = window.event ? e.keyCode : e.which;

 if ((key > 95 && key < 106) || //小鍵盤上的0到9

 (key > 47 && key < 60) || //大鍵盤上的0到9

 (key == 110 && value.indexOf(".") < 0) || //小鍵盤上的.而且以前沒有輸入.

 (key == 190 && value.indexOf(".") < 0) || //大鍵盤上的.而且以前沒有輸入.

 key == 8 || key == 9 || key == 46 || key == 37 || key == 39 //不影響正常編輯鍵的使用(8:BackSpace;9:Tab;46:Delete;37:Left;39:Right)

) {

  isOK = true;

 } else {

  if (window.event) //IE

  {

   e.returnValue = false; //event.returnValue=false 效果相同.

  }

  else //Firefox

  {

   e.preventDefault();

  }

 }

 return isOK;

}

//只允許輸入數(shù)字

function checkKeyForInt(value, e) {

 var isOK = false;

 var key = window.event ? e.keyCode : e.which;

 if ((key > 95 && key < 106) || //小鍵盤上的0到9

 (key > 47 && key < 60) || //大鍵盤上的0到9

 key == 8 || key == 9 || key == 46 || key == 37 || key == 39 //不影響正常編輯鍵的使用(8:BackSpace;9:Tab;46:Delete;37:Left;39:Right)

) {

  isOK = true;

 } else {

  if (window.event) //IE

  {

   e.returnValue = false; //event.returnValue=false 效果相同.

  }

  else //Firefox

  {

   e.preventDefault();

  }

 }

 return isOK;

}

//設(shè)置有自定義屬性 dtype 的文本框 允許輸入的范圍

function setDType() {

 $(":text[dtype]").each(function () {

  var dtype = $(this).attr("dtype");

  var isOK = true;

  switch (dtype) {

   case "number":

    $(this).css("ime-mode", "disabled").keydown(function (event) {

     isOK = checkKeyForFloat($(this).val(), event);

     if (!isOK) {

      //$(this).SuperFocus("", 500);

     }

     return isOK;

    });

    break;

   default:

    break;

  }

 });

}

</script>

<script type="text/javascript">

$(function () {

 setDType();

});

</script>

</head>

<body>

年齡: <input type="text" maxlength="3" onkeydown="checkKeyForInt(this.value,event)" style="ime-mode: disabled"/><br />

身高:<input type="text" maxlength="5" dtype="number" />

</body>

</html>

希望本文所述對大家jQuery程序設(shè)計(jì)有所幫助。

更多信息請查看網(wǎng)絡(luò)編程
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)