欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長(zhǎng)資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          javascript怎樣設(shè)置文本框不能輸入數(shù)字

          在JavaScript中,可以利用if語(yǔ)句和keycode來(lái)設(shè)置文本框不能輸入數(shù)字,0到9的keycode值為48到57,只要控制文本框內(nèi)的keycode值不在該范圍內(nèi),語(yǔ)法為“if(charCode>46&&charCode<58)”。

          javascript怎樣設(shè)置文本框不能輸入數(shù)字

          本教程操作環(huán)境:windows10系統(tǒng)、javascript1.8.5版、Dell G3電腦。

          javascript怎樣設(shè)置文本框不能輸入數(shù)字

          數(shù)字0-9keycode的值為48-57,只要在JavaScript中設(shè)置文本框中輸入keycode的值在這個(gè)范圍內(nèi)就就取消此行為,以此就可以實(shí)現(xiàn)文本框不能輸入數(shù)字。

          示例如下:

          javascript怎樣設(shè)置文本框不能輸入數(shù)字

          或者:

          <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <meta http-equiv="X-UA-Compatible" content="ie=edge">     <title>Document</title> </head> <body> <input type="text" id="test">     <script type="text/javascript">     window.onload=function(){              document.getElementById('test').addEventListener('keypress',function(e){              var charCode=e.charCode;              if(charCode>46&&charCode<58)  /*0-9 的charcode*/              e.preventDefault();      });     }     </script> </body> </html>

          輸出結(jié)果與上述結(jié)果相同,文本框內(nèi)無(wú)法輸入數(shù)字。

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)