zoukankan      html  css  js  c++  java
  • jquery只能输入数字

    <html>
    <head>
    <title>1st</title>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"></meta>
    <script src="js/jquery-1.8.3.js" type="text/javascript">
    /**window.onload=function(){
    document.getElementById("1st").innerHTML="helloWorld";
    }**/
    </script>
    // imeMode有四种形式,分别是:
    // active 代表输入法为中文
    // inactive 代表输入法为英文
    // auto 代表打开输入法 (默认)
    // disable 代表关闭输入法
    //同事收藏的做法:
    //1.正则表达式限制Float:(^[0-9]([.][0-9]{1,2})?$)|(^1[0-9]([.][0-9]{1,2})?$)|(^2[0-3]([.][0-9]{1,2})?$)|(^24([.]0{1,2})?$)
    //2.限制输入
    //function KeyPress(objTR) {//只允许录入数据字符 0-9 和小数点
    // //var objTR = element.document.activeElement;
    // var txtval = objTR.value;
    // var key = event.keyCode;
    // if ((key < 48 || key > 57) && key != 46) {
    // event.keyCode = 0;
    // alert("只能输入数字");
    // }
    // else {
    // if (key == 46) {
    // if (txtval.indexOf(".") != -1 || txtval.length == 0)
    // event.keyCode = 0;
    // }
    // }
    // }
    //3.禁止切换输入法
    //Style: "ime-mode: disabled"
    <script type="text/javascript">
    $(function(){
    $("#hjg").keydown(function(e){   
        // 注意此处不要用keypress方法,否则不能禁用Ctrl+C与Ctrl+V,具体原因请自行查找keyPress与keyDown区分,十分重要,请细查
    if ($.browser.msie) { // 判断浏览器
    if ( ((event.keyCode > 47) && (event.keyCode < 58)) || (event.keyCode == 8) ) {  // 判断键值
    return true;
    } else {
    return false;
    }
    } else {
    if ( ((e.which > 47) && (e.which < 58)) || (e.which == 8) || (event.keyCode == 17) ) {
    return true;
    } else {
    return false;
    }
    }}).focus(function() {
    this.style.imeMode='disabled'; // 禁用输入法,禁止输入中文字符

       });
    });

    </script>
    <style type="text/css">

    </style>
    </head>
    <body>
    <input type="text" id="hjg" style="height:50px;weight:250px">
    </body>
    </html>

  • 相关阅读:
    vue-动画
    vue笔记-路由,组件
    自定义键盘信息
    自定义指令
    vue-笔记2
    轻松搭建基于 Serverless 的文档图片在线转换服务
    轻松搭建基于 SpringBoot + Vue 的 Web 商城应用
    一小时快速搭建基于阿里云容器服务-Kubernetes的Web应用
    阿里云正式推出内容平台“云栖号”:全面助力企业和个人上云决策
    云原生安全-更安全的密文管理 Vault on ACK
  • 原文地址:https://www.cnblogs.com/pond/p/5365430.html
Copyright © 2011-2022 走看看